简体   繁体   English

如何使这个jQuery div滚动条在同一个类的多个div上工作?

[英]How can I make this jQuery div scroller work on multiple divs with the same class?

I've been using this jQuery div scroll script ( How to make a scrolable div scroll on click and mouseover using jQuery ) to good effect, but now my client wants me to use it within a Wordpress blog page where there are multiple areas on the page that need scrolling. 我一直在使用此jQuery div滚动脚本( 如何使用jQuery在单击和鼠标悬停时进行可滚动的div滚动 )效果良好,但现在我的客户希望我在Wordpress博客页面(其中有多个区域)中使用它需要滚动的页面。

Is it possible to use this script on multiple instances with the same class (Ie; 'scroll')? 是否可以在具有相同类(即“滚动”)的多个实例上使用此脚本?

This is my script; 这是我的剧本;

$(function() {
var ele   = $('.scroll');
var speed = 25, scroll = 5, scrolling;

$('#scroll-up').mouseenter(function() {
    // Scroll the element up
    scrolling = window.setInterval(function() {
        ele.scrollTop( ele.scrollTop() - scroll );
    }, speed);
});

$('#scroll-down').mouseenter(function() {
    // Scroll the element down
    scrolling = window.setInterval(function() {
        ele.scrollTop( ele.scrollTop() + scroll );
    }, speed);
});

$('#scroll-up, #scroll-down').bind({
    click: function(e) {
        // Prevent the default click action
        e.preventDefault();
    },
    mouseleave: function() {
        if (scrolling) {
            window.clearInterval(scrolling);
            scrolling = false;
        }
    }
});
});

and this is the markup (Both the Main "feed" wrapper and the individual "single" divs need to be scrollable); 这就是标记(主“提要”包装器和单个“单个” div都需要滚动);

<div class="feed scroll">

    <div class="single scroll">
        <!-- single blog post content -->
    </div>

    <div class="single scroll">
        <!-- single blog post content -->
    </div>

    <div class="single scroll">
        <!-- single blog post content -->
    </div>

</div>

<a id="scroll-up" href="#">Scroll Up</a>
<a id="scroll-down" href="#">Scroll Down</a>

So basically I need to be able to scroll everything individually. 因此,基本上我需要能够逐个滚动所有内容。

If you want multiple sections, you'll have to use classes, rather than ids for identifying the sections, then you'll have to traverse the DOM from your link to the content to scroll in order to find the right div. 如果需要多个部分,则必须使用类而不是ID来标识这些部分,然后必须遍历DOM(从链接到要滚动的内容)以找到正确的div。 Also, you'll have to store the 'scrolling' status individually for each element that you're scrolling. 另外,您必须为要滚动的每个元素分别存储“滚动”状态。

That's a lot of stuff to explain verbally. 口头解释有很多东西。 Here's an example, modified from the question you referenced: http://jsfiddle.net/Qp8bB/ 这是一个示例,该示例是根据您引用的问题进行修改的: http : //jsfiddle.net/Qp8bB/

Note: 注意:

$(this).siblings(".content")

This is how we navigate from the link element to the content 这就是我们从链接元素导航到内容的方式

element.attr('data-scrolling', 'true');

This is how we store the scrolling status of each element (via a temporary attribute) 这就是我们存储每个元素的滚动状态的方式(通过临时属性)

Not the cleanest code, but I hope it gets the point across. 不是最干净的代码,但我希望它能使您明白这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM