鼠标悬停在div上运行堆栈插件

时间:2012-04-19 03:35:49

标签: javascript jquery hover

我正在使用以下插件

http://playground.mobily.pl/jquery/mobily-notes/demo.html

它提供了一个非常好的堆栈,但问题是当我将它用于我的库时。所有专辑都是自动旋转,看起来很奇怪。在我们将鼠标悬停在div上而不是自动运行之后,是否有任何可能的方法至少运行插件?运行它的主要代码是

$(function(){
    $('.notes_img').mobilynotes({
        init: 'rotate',
        showList: false,
        positionMultiplier: 5
    });
});

3 个答案:

答案 0 :(得分:2)

注意:我不是作者,但它是MIT许可的插件,所以在这里修改和重新分发它应该没有任何问题。

尽管插件有点眼睛,它的弹性不足以伸展。 您可以改用我的修改版本。

mobilynotes.js:

(function ($) {
    $.fn.mobilynotes = function (options) {
        var defaults = {
            init: "rotate",
            positionMultiplier: 5,
            title: null,
            showList: true,
            autoplay: true,
            interval: 4000,
            hover:    true,
            index:    1
        };
        var sets = $.extend({}, defaults, options);
        return this.each(function () {
            var $t = $(this),
                note = $t.find(".note"),
                size = note.length,
                autoplay;
            var notes = {
                init: function () {
                    notes.css();
                    if (sets.showList) {
                        notes.list()
                    }
                    if (sets.autoplay) {
                        notes.autoplay()
                    }
                    if (sets.hover) {
                        notes.hover()
                    }
                    notes.show()
                },
                random: function (l, u) {
                    return Math.floor((Math.random() * (u - l + 1)) + l)
                },
                css: function () {
                    var zindex = note.length;
                    note.each(function (i) {
                        var $t = $(this);
                        switch (sets.init) {
                        case "plain":
                            var x = notes.random(-(sets.positionMultiplier), sets.positionMultiplier),
                                y = notes.random(-(sets.positionMultiplier), sets.positionMultiplier);
                            $t.css({
                                top: y + "px",
                                left: x + "px",
                                zIndex: zindex--
                            });
                            break;
                        case "rotate":
                            var rotate = notes.random(-(sets.positionMultiplier), sets.positionMultiplier),
                                degrees = "rotate(" + rotate + "deg)";
                            $t.css({
                                "-webkit-transform": degrees,
                                "-moz-transform": degrees,
                                "-o-transform": degrees,
                                filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + rotate + ")",
                                zIndex: zindex--
                            })
                        }
                        $t.attr("note", i)
                    })
                },
                zindex: function () {
                    var arr = new Array();
                    note.each(function (i) {
                        arr[i] = $(this).css("z-index")
                    });
                    var z = Math.max.apply(Math, arr);
                    return z
                },
                list: function () {
                    $t.after($("<ul />").addClass("listNotes"));
                    var ul = $t.find(".listNotes"),
                        title = new Array();
                    if (sets.title != null) {
                        note.each(function (i) {
                            title[i] = $(this).find(sets.title).text()
                        })
                    } else {
                        title[0] = "Bad selector!"
                    }
                    for (x in title) {
                        $t.next(".listNotes").append($("<li />").append($("<a />").attr({
                            href: "#",
                            rel: x
                        }).text(title[x])))
                    }
                },
                autoplay: function () {
                    var i = 1,
                        autoplay = setInterval(function () {
                            i == size ? i = 0 : "";
                            var div = note.eq(i),
                                w = div.width(),
                                left = div.css("left");
                            notes.animate(div, w, left);
                            i++
                        }, sets.interval)
                },
                hover: function () {
                    $t.hover(function() {
                        var div = note.eq(sets.index),
                        w = div.width(),
                        left = div.css("left");
                        sets.index == size ? sets.index = 1 : sets.index += 1;
                        notes.animate(div, w, left);
                    },
                    function() {}
                    );
                },
                show: function () {
                    $t.next(".listNotes").find("a").click(function () {
                        var $t = $(this),
                            nr = $t.attr("rel"),
                            div = note.filter(function () {
                                return $(this).attr("note") == nr
                            }),
                            left = div.css("left"),
                            w = div.width(),
                            h = div.height();
                        clearInterval(autoplay);
                        notes.animate(div, w, left);
                        return false
                    })
                },
                animate: function (selector, width, position) {
                    var z = notes.zindex();
                    selector.animate({
                        left: width + "px"
                    }, function () {
                        selector.css({
                            zIndex: z + 1
                        }).animate({
                            left: position
                        })
                    })
                }
            };
            notes.init()
        })
    }
}(jQuery));

使用新功能:

$('.notes_img').mobilynotes({
        init: 'rotate',
        showList: false,
        autoplay: false,
        index: 1, //starting index (new)
        hover: true // (new)
    });

答案 1 :(得分:2)

接管@username停止的地方(优秀的工作),我已经分配用户名的小提琴,对配置选项进行了以下更改:

修改(来自@ username的代码):

    悬停时
  • hover :(布尔值),反转autoplay
  • 的效果

新:

    点击
  • click :(布尔值),前进到下一个音符,然后在悬停状态下恢复自动播放(如果处于活动状态)。

在内部,新的nextstoprestart函数以及修改后的initautoplayanimate函数处理(组合)选项。

最棘手的部分是在animate中提供回调,以便在next(点击操作)完成后恢复自动播放。这在其他几个功能中产生了影响。 (反思无疑是使用deferreds更好的方法 - 我会考虑一下这一点)

这是fiddle(或this full page version),其设置反映了@Sakshi Sharma的原始问题。我将click设置为true,但同样可以设置为false,具体取决于首选项。

这是代码:

(function($) {
    $.fn.mobilynotes = function(options) {
        var defaults = {
            init: "rotate",
            positionMultiplier: 5,
            title: null,
            showList: true,
            autoplay: false,
            hover: true,//when true, hovering  reverses autoplay; when false, has no effect.
            click: true,
            interval: 4000,
            index: 1
        };
        var settings = $.extend({}, defaults, options);
        return this.each(function() {
            var $t = $(this),
                note = $t.find(".note"),
                size = note.length,
                autoplay,
                currentIndex = 1;
            var notes = {
                init: function() {
                    notes.css();
                    if (settings.showList) {
                        notes.list();
                    }
                    if (settings.hover) {
                        var fn1 = settings.autoplay ? notes.stop : notes.restart;
                        var fn2 = settings.autoplay ? notes.restart : notes.stop;
                        $t.hover(fn1, fn2);
                    }
                    if (settings.click) {
                        clearInterval(autoplay);
                        //autoplay 0, hover 0: null
                        //autoplay 0, hover 1: autoplay
                        //autoplay 1, hover 0: autoplay
                        //autoplay 1, hover 1: null
                        var callback = ( (settings.autoplay && !settings.hover) || (!settings.autoplay && settings.hover) ) ? notes.autoplay : null;
                        $t.click(function(){
                            notes.next(callback);
                        });
                    }
                    if (settings.autoplay) {
                        notes.autoplay();
                    }
                    notes.show();
                },
                random: function(l, u) {
                    return Math.floor((Math.random() * (u - l + 1)) + l);
                },
                css: function() {
                    var zindex = note.length;
                    note.each(function(i) {
                        var $t = $(this);
                        switch (settings.init) {
                        case "plain":
                            var x = notes.random(-(settings.positionMultiplier), settings.positionMultiplier),
                                y = notes.random(-(settings.positionMultiplier), settings.positionMultiplier);
                            $t.css({
                                top: y + "px",
                                left: x + "px",
                                zIndex: zindex--
                            });
                            break;
                        case "rotate":
                            var rotate = notes.random(-(settings.positionMultiplier), settings.positionMultiplier),
                                degrees = "rotate(" + rotate + "deg)";
                            $t.css({
                                "-webkit-transform": degrees,
                                "-moz-transform": degrees,
                                "-o-transform": degrees,
                                filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + rotate + ")",
                                zIndex: zindex--
                            })
                        }
                        $t.attr("note", i)
                    });
                },
                zindex: function() {
                    var arr = new Array();
                    note.each(function(i) {
                        arr[i] = $(this).css("z-index")
                    });
                    var z = Math.max.apply(Math, arr);
                    return z
                },
                list: function() {
                    $t.after($("<ul />").addClass("listNotes"));
                    var ul = $t.find(".listNotes"),
                        title = new Array();
                    if (settings.title != null) {
                        note.each(function(i) {
                            title[i] = $(this).find(settings.title).text()
                        })
                    } else {
                        title[0] = "Bad selector!"
                    }
                    for (x in title) {
                        $t.next(".listNotes").append($("<li />").append($("<a />").attr({
                            href: "#",
                            rel: x
                        }).text(title[x])))
                    }
                },
                next: function(callback) {
                    callback = (!callback || typeof callback !== 'function') ? null : callback;
                    currentIndex = currentIndex % size;
                    notes.animate(note.eq(currentIndex), callback);
                    currentIndex++;
                },
                autoplay: function() {
                    notes.stop();
                    autoplay = setInterval(notes.next, settings.interval);
                },
                stop: function() {
                    clearInterval(autoplay);
                },
                restart: function() {
                    notes.next(notes.autoplay);
                },
                show: function() {
                    $t.next(".listNotes").find("a").click(function() {
                        var $t = $(this),
                            nr = $t.attr("rel"),
                            div = note.filter(function() {
                                return $(this).attr("note") == nr;
                            });
                        clearInterval(autoplay);
                        notes.animate(div);
                        return false;
                    })
                },
                animate: function(selector, callback) {
                    var width = selector.width(),
                        position = selector.css("left"),
                        z = notes.zindex();
                    selector.animate({
                        left: width + "px"
                    }, function() {
                        selector.css({
                            zIndex: z + 1
                        }).animate({
                            left: position
                        }, function(){
                            if(callback) {
                                callback();
                            }
                        });
                    });
                }
            };
            notes.init()
        })
    }
}(jQuery));

答案 2 :(得分:0)

Hiya那里演示 :)并希望这会有所帮助:http://jsfiddle.net/haf6J/14/show/&amp;&amp; http://jsfiddle.net/haf6J/14/http://jsfiddle.net/haf6J/3/show/&amp;&amp; http://jsfiddle.net/haf6J/3/

现在旋转将在您悬停图像时开始,如果您想要进一步停止一个鼠标,它应该停止,您可以查看事件.mouseout,您可以停止旋转,即删除效果。

就像epascarello提到的文档在这里http://playground.mobily.pl/jquery/mobily-notes.html

请让我知道,如果这有助于(和upvote :))欢呼,请不要忘记接受

Jquery代码

$('.notes_img').hover(function() {
    $('.notes_img').mobilynotes({
        init: 'rotate',
        showList: false
    });

});​