开发者

jquery mouseenter fires continuosly

开发者 https://www.devze.com 2023-03-24 18:28 出处:网络
I have a mousenter/mouseleave function that swaps out content when the element is being hovered over. however instead of it firing only on mouseenter it seems to ke开发者_Python百科ep continuously fir

I have a mousenter/mouseleave function that swaps out content when the element is being hovered over. however instead of it firing only on mouseenter it seems to ke开发者_Python百科ep continuously firing. I have created a jsfiddle here so hopefully it will be easier to understand.


Inside your rotate_start() and rotate_reset() functions you are triggering a hover event via this line:

jq('.product-image-container',super_prod).attr('hover','true');

Per JQuery docs, hover() method binds handlers for both mouseenter and mouseleave events. You're basically applying recursion unintentionally, as hover is fired as the mouse moves.

A simple fix for this is to replace .attr('hover','true'); with .data('hover',true);

Here is a scalable solution which should accomplish your objective:

HTML:

<div class="item">
    <ul>
        <li>
            Main product photo
        </li>
        <li>
            Alternate product photo 1
        </li>
        <li>
            Alternate product photo 2
        </li>
        <li>
            Alternate product photo 3
        </li>
    </ul>
</div>

CSS:

.item {
    width:200px;
    height:200px;
    background-color:#F0F0F0;
    border:1px solid #666;
}
.item ul li {
    display:none;
}

JQuery:

var jq = jQuery.noConflict();
var timers = [];
var interval_seconds = 2;

jq('.item').mouseenter(function() {

    // Scope this
    var _this = jq(this);

    // Self-executing function
    (function cycle() {

        // Recursive setTimeout accommodates for animation time - setInterval wont
        timers['item'] = setTimeout(function() {

            // Grab the currently visible photo, and the next photo
            var _this_li = _this.find('li:visible');
            var _next_li = _this_li.next();

            // If there is no next photo, cycle back to first photo
            if (_next_li.length == 0) {
                _next_li = _this.find('li:first-child');
            }

            // Animate the rotation
            _this_li.fadeOut('slow', function() {
                _next_li.fadeIn('slow', function() {

                    // Recursion
                    cycle();
                });
            });
        }, interval_seconds * 1000);
    })();
});

jq('.item').mouseleave(function() {

    // Stop the recursive setTimeout
    clearTimeout(timers['item']);

    // Scope this
    var _this = jq(this);

    // Grab the first photo in the list
    var _first_li = _this.find('li:first-child');

    // If the first photo in the list is not the currently visible photo, make it so.
    if (_first_li.not(':visible')) {
        _this.find('li:visible').fadeOut('slow', function() {
            _first_li.fadeIn('slow');
        });
    }
});

jq(function() {

    // Show the first photo on load
    jq('.item').find('li:first-child').fadeIn('slow');
});

Demo: http://jsfiddle.net/AlienWebguy/EY8mM/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消