Rails 5 / jQuery:点击touchstart事件不起作用

时间:2018-05-20 23:09:26

标签: jquery ruby-on-rails ruby-on-rails-5 turbolinks-5

我正在尝试使用波本威士忌/替换物组件(sliding_panel)并且我有on('click touchstart')事件。我正在使用它来导航菜单。该事件将在第一页加载时正常工作,但之后将停止。该活动使用toggleClass来显示和隐藏菜单。我不确定这是否是一个turbolinks问题所以我做了一些挖掘并找到了jquery-turbolinks宝石。我完成了安装说明,但结果相同。有没有其他人有这个问题?

以下代码:

_navbar.html.erb

<button type="button" class="js-menu-trigger sliding-panel-button">
 Click for Sliding Panel
</button>

<nav class="js-menu sliding-panel-content">
 <ul>
  <li><%= link_to 'Users', users_path %></li>
  <li><a href="javascript:void(0)">Item 2</a></li>
  <li><a href="javascript:void(0)">Item 3</a></li>
 </ul>
</nav>

<div class="js-menu-screen sliding-panel-fade-screen"></div>

sliding_panel.js

$(document).ready(function(){
 $('.sliding-panel-button,.sliding-panel-fade-screen,.sliding-panel-close').on('click touchstart',function (e) {
 $('.sliding-panel-content,.sliding-panel-fade-screen').toggleClass('is-visible');
 e.preventDefault();
 });
});

sliding_panel.scss

.sliding-panel-content {
  $action-color: #477DCA !default;
  $dark-gray: #333 !default;
  $sliding-panel-border-color: $dark-gray;
  $sliding-panel-background: lighten($sliding-panel-border-color, 5%);
  $sliding-panel-color: #fff;
  $sliding-panel-border: 1px solid $sliding-panel-border-color;
  $sliding-panel-background-hover: $action-color;
  $sliding-panel-color-hover: #fff;
  $sliding-panel-background-focus: lighten($sliding-panel-background, 5%);

  @include position(fixed, 0 auto 0 0);
  @include size(220px, 100%);
  background: $sliding-panel-background;
  -webkit-overflow-scrolling: touch;
  overflow-y: auto;
  transform: translateX(-220px);
  transition: all 0.25s linear;
  z-index: 999999;

  ul {
    padding: 0;
    margin: 0;
  }

  li {
    list-style: none;
  }

  li a {
    border-bottom: $sliding-panel-border;
    color: $sliding-panel-color;
    display: block;
    font-weight: bold;
    padding: 1em;
    text-decoration: none;

    &:focus {
      background-color: $sliding-panel-background-focus;
    }

    &:hover {
      background-color: $sliding-panel-background-hover;
      color: $sliding-panel-color-hover;
    }
  }

  &.is-visible {
    transform: translateX(0);
  }
}

.sliding-panel-fade-screen {
  @include position(fixed, 0);
  background: black;
  opacity: 0;
  transition: all 0.2s ease-in-out;
  visibility: hidden;
  z-index: 999998;

  &.is-visible {
    opacity: 0.4;
    visibility: visible;
  }
}

.sliding-panel-button {
  cursor: pointer;
  display: inline-block;
  outline: none;
  padding: 10px 16px;
  position: relative;

  img {
    height: 1.3em;
  }
}

这些完全是从bourbon/refills site here完全复制的。

2 个答案:

答案 0 :(得分:0)

您不应该听取就绪事件,而应该听page:load事件。所以,代码将是这样的:

$(document).on('page:load', function(){
 $('.sliding-panel-button,.sliding-panel-fade-screen,.sliding-panel-close').on('click touchstart',function (e) {
 $('.sliding-panel-content,.sliding-panel-fade-screen').toggleClass('is-visible');
 e.preventDefault();
 });
});

来源:Rails 4: how to use $(document).ready() with turbo-links

希望它有所帮助。

答案 1 :(得分:0)

好的,所以我有Turbolinks~&gt; 5.2。我回到了Turbolinks~&gt; 5.0.1然后加载jQuery,如下所示:$(document).on('turbolinks:load', function()而不是ready()This SO post帮助我找到解决方案。