【问题标题】:Pass Unique-ids to Javascript for Horizontal Scrolling将唯一 ID 传递给 Javascript 以进行水平滚动
【发布时间】:2015-02-03 02:44:35
【问题描述】:

我正在尝试开发一个类似于 Netflix 的水平滚动界面

一切都显示并正常工作,但由于某种原因,Javascript 仅滚动浏览索引页面中的第一本出版商书籍。当我尝试悬停和滚动任何其他出版商书籍时,它只会水平滚动第一出版商书籍。

我知道我有很多次出现#scroll,这就是为什么 jQuery 只看到第一个。

有谁知道我可以如何传递唯一的 publisher_id 或类,以便它适用于所有发布者?

模型

class Publisher < ActiveRecord::Base

  has_many :characters
  has_many :books, :through => :characters

end


class Character < ActiveRecord::Base

  belongs_to :publisher
  has_many :books

end

class Book < ActiveRecord::Base

  belongs_to :character

end

观看次数

books.html.erb

  <%# This lists all the publishers %>
  <div class="publisherbubble">

    <% @publishers.each do |publisher| %>

      <div class = "publisherbox">
        <div class = "slider triangleBtns">

          ###renders all the books
          <%= render :partial => 'static_pages/books', :locals =>{:publisher => publisher} %>

        </div>  
      </div>

    <% end %>

  </div>

_books.html.erb

  ###How can I pass/use a unique ID or class to make this work?
  <div class="scroll-container">

      <ul class="scrollertitle">     

        <% publisher.characters.sort_by{|character| character.created_at }.each do |character|%>
          <% character.books.each do |book| %>
            <li>
              <%= link_to (image_tag book.photo(:small)), 
                  publisher_character_book_issues_path(:publisher_id => publisher.id, 
                  :character_id => character.id, :book_id => book.id ) %>
            </li>
          <% end %>
        <% end %>

      </ul>

      <span class="previous sliderButton" data-scroll-modifier='-1'>
        <div class="arrow">
        </div>
      </span>

      <span class="next sliderButton" data-scroll-modifier='1'>
        <div class="arrow">
        </div>
      </span>

  </div>

Javascript

$(function () {

    var scrollHandle = 0,
        scrollStep = 5,

        ###How can I pass/use a unique ID or class to make this work?
        parent = $(this).closest('.scroll-container');

    //Start the scrolling process
    $(".sliderButton").on("mouseenter", function () {
        var data = $(this).data('scrollModifier'),
            direction = parseInt(data, 10);

        $(this).addClass('active');

        startScrolling(direction, scrollStep, parent);
    });

    //Kill the scrolling
    $(".sliderButton").on("mouseleave", function () {
        stopScrolling();
        $(this).removeClass('active');
    });

    //Actual handling of the scrolling
    function startScrolling(modifier, step, parent) {
        if (scrollHandle === 0) {
            scrollHandle = setInterval(function () {
                var newOffset = parent.scrollLeft() + (scrollStep * modifier);

                parent.scrollLeft(newOffset);
            }, 10);
        }
    }

    function stopScrolling() {
        clearInterval(scrollHandle);
        scrollHandle = 0;
    }

});

CSS

.scroll-container {
  width:auto;
  height: 100%;
  background: transparent;
  overflow-y: hidden;
  overflow-x: scroll;
  -webkit-overflow-scrolling: touch;
  margin-top: 5px;
}

【问题讨论】:

  • 水平滚动!我不得不实施一次。它在这个页面的底部:dabble.co

标签: javascript jquery html css ruby-on-rails


【解决方案1】:

首先,我会给父 div 一个像 scroll-container 这样的类。

然后,我会将父 div 作为参数传递给startScrolling

parent = $(this).closest('.scroll-container')
startScrolling(direction, scrollStep, parent)

那么,你就可以访问父级,而不必将其设置在 js 的顶部。

顺便说一句,如果您将顶部的scrollStep 设置为一种可配置的常量,则无需将其作为参数传递。 startScrolling 看起来没有它应该可以正常工作。

另一方面,我可以看到 startScrolling 只接受一个参数:父 div。 data-modifier 可以住在那里,而不必住在两个地方。您可以在 startScrolling 函数中从父级获取修饰符。

更新

$(function () {

    var scrollHandle = 0,
        scrollStep = 5;

    //Start the scrolling process
    $(".sliderButton").on("mouseenter", function () {
        var data = $(this).data('scrollModifier'),
            direction = parseInt(data, 10);

        $(this).addClass('active');
        parent = $(this).closest('.scroll-container');
        startScrolling(direction, scrollStep, parent);
    });

    //Kill the scrolling
    $(".sliderButton").on("mouseleave", function () {
        stopScrolling();
        $(this).removeClass('active');
    });

    //Actual handling of the scrolling
    function startScrolling(modifier, step, parent) {
        if (scrollHandle === 0) {
            scrollHandle = setInterval(function () {
                var newOffset = parent.scrollLeft() + (scrollStep * modifier);

                parent.scrollLeft(newOffset);
            }, 10);
        }
    }

    function stopScrolling() {
        clearInterval(scrollHandle);
        scrollHandle = 0;
    }

});

【讨论】:

  • 我已经按照建议更新了上面的答案,我有两个问题,这需要我更改 css 吗?我是否还需要传递publisher_id,以便滚动函数确切知道要滚动哪个发布者?
  • 是的,将 css 从 #scroll 更改为 .scroll-container 或您最终选择的任何类。您不需要传递发布者 ID,因为您正在根据悬停的特定元素(最近)找到正确的父级。您还应该从 div 中删除#scroll;一个页面上有多个相同的 id 是不好的 html。
  • 是的,我已经更新了代码,但现在没有一个可以工作......:/也许看到 html 会有所帮助......
  • 你几乎拥有它。我更新了答案以包含更大的上下文。
  • 噢,明白了。非常感谢,我真的很感激。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
相关资源
最近更新 更多