0

我正在使用以下插件

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

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

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

3 回答 3

2

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

尽管插件很吸引人,但它的弹性不足以扩展。你可以改用我的修改版。

mobilenotes.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)
    });
于 2012-04-19T05:15:08.113 回答
2

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

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

  • hover: (boolean) 悬停时,反转效果autoplay

新的:

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

next在内部,newstop和函数以及restartmodifiedinit和函数处理选项(的组合)。autoplayanimate

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

这是小提琴(或这个完整页面版本),其设置反映了@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));
于 2012-04-20T00:49:23.000 回答
0

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

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

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

请让我知道,如果这有帮助,请不要忘记接受(并投票:))干杯

jQuery代码

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

});​
于 2012-04-19T04:00:38.557 回答