Continuously scroll jCarousel on button hover

。_饼干妹妹 提交于 2019-12-14 04:01:27

问题


I'm using the jCarousel plugin and I've hit a road bump...

I need the carousel to continuously scroll whenever the navigation buttons are hovered. Changing the built-in configuration variables to "mouseover" just scrolls once per hover.

I came across this similar question but I'm not a javascript expert and can't get the answer to work.

Here's my code:

    function mycarousel_initCallback(carousel)
{

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        auto: 10,
        start: 1,
        scroll: 1,
        animation: 'slow',
        wrap: 'circular',
        buttonNextEvent: 'mouseover',
        buttonPrevEvent: 'mouseover',
        initCallback: mycarousel_initCallback
    });
});

Any help would be much appreciated.


回答1:


You can use the following script to make it work. I have tested it on jquery.jcarousel.js and jquery-1.4.1

To note, my jcarousel settings did not have auto scroll.

<script>
jQuery(document).ready(function() {
    var _this = null;
    $('.jcarousel-next').mouseover(function() {
        if (!$(this).hasClass("jcarousel-next-disabled")) {
            _this = $(this);
            _this.click();
            window.setTimeout(CallAgain, 100);
        }
    });

    $('.jcarousel-next').mouseout(function() {
        if (!$(this).hasClass("jcarousel-next-disabled")) {
            _this = null;
        }
    });

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

    $('.jcarousel-prev').mouseover(function() {
        if (!jQuery(this).hasClass("jcarousel-prev-disabled")){
            _this = $(this);
            _this.click();
            window.setTimeout(CallAgain, 100);
        }
    });

    $('.jcarousel-prev').mouseout(function() {
        if (!$(this).hasClass("jcarousel-next-disabled")) {
            _this = null;
        }
    });
});
</script>


来源:https://stackoverflow.com/questions/8143599/continuously-scroll-jcarousel-on-button-hover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!