简体   繁体   中英

Continuously scroll jCarousel on button hover

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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