简体   繁体   English

jQuery使用侦听器在mousedown上滚动(适用于jCarousel)

[英]jQuery scroll on mousedown with listener (for jCarousel)

I'm trying to use jQuery jCarousel as an image browser. 我正在尝试使用jQuery jCarousel作为图像浏览器。 It's working fine, but I'd like to change the interaction so that it scrolls continuously on mousedown, rather than scrolling the set amount on click. 它工作正常,但我想更改交互方式,以使其在鼠标按下时连续滚动,而不是在单击时滚动设置的数量。

Obviously, I'm a complete novice using jQuery, but I initially thought I could use the jCarousel configuration event option to trigger the control at mousedown. 显然,我是使用jQuery的完全新手,但我最初认为我可以使用jCarousel配置事件选项在鼠标按下时触发控件。 This works, but doesn't continue the scrolling. 这可行,但不会继续滚动。

I think I need a listener to see if the mouse is still down, right? 我想我需要一个监听器来查看鼠标是否仍在按下,对吗? I found this solution on StackOverflow and am trying to apply: jsfiddle.net/amenity/BSq85/19 我在StackOverflow上找到了解决方案,并尝试应用: jsfiddle.net/amenity/BSq85/19

jQuery(document).ready(function () {
    $('.jcarousel').jcarousel({
        wrap: 'circular',
        animation: 1500,
        easing: 'linear'
    });

    var timeout, clicker = $('.jcarousel-prev');
    var count = 0;

    clicker.mousedown(function () {
        timeout = setInterval(function () {
            $('.jcarousel-prev').jcarouselControl({
                target: '-=2'
            });
        }, 500);

        return false;
    });

    $(document).mouseup(function () {
        clearInterval(timeout);
        return false;
    });

    $('.jcarousel-next').jcarouselControl({
        target: '+=2',
            'event': 'mousedown'
    });


});

I left the (right) next button moving on click for comparison to the functioning external control. 我单击鼠标左键(右)以与功能外部控件进行比较。

Without anyone to pull me out of the newbie abyss, I floundered until I go something. 没有人把我从新手深渊中拉出来,我挣扎了直到我走了。 This answer looked so close to what I needed that I used it as a starting point. 这个答案看起来非常接近我所需要的,因此我以它为起点。 Now I've at least gotten here . 现在我至少到了这里

The key is, as I suspected, creating a timer to call the function again if the mouse is still down and clearing it on mouseup: 正如我所怀疑的那样,关键是创建一个计时器,以在鼠标仍然按下时再次调用该函数,并在mouseup上将其清除:

 var _this = null;
    $('.jcarousel-next').mousedown(function () {
        $('.jcarousel-next').jcarouselControl({
              target: '+=2'
        });
        _this = $(this);
        _this.click();
        window.setTimeout(CallAgain, 100);
    });

    $('.jcarousel-next').mouseup(function () {
            _this = null;
        });

    function CallAgain() {
        if (_this != null) {
            //alert("Inside Call again");
            _this.click();
            window.setTimeout(CallAgain, 100);
        }
    };

On the fiddle, the right arrow has the scroll on mousedown, while left/prev is left at the default. 在小提琴上,向右箭头使鼠标向下滚动,而默认情况下向左/向左滚动。 Now I just have to smooth it out. 现在,我只需要解决它。

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

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