开发者

Jquery - Safari won't register mousemove in plugin

开发者 https://www.devze.com 2023-04-03 20:36 出处:网络
Simply put, it works the first few times I test it, then without any change whatsoever to the code it stops. Here it\'s posted on jsFiddle it\'s not working at all on it but here\'s the link: http://j

Simply put, it works the first few times I test it, then without any change whatsoever to the code it stops. Here it's posted on jsFiddle it's not working at all on it but here's the link: http://jsfiddle.net/x24Te/

And here's the code

(function($) {
$.fn.jqScroll = function(options) {
    var $this = this;
    var origin, current, difference;
    var height = this.outerHeight();
    var width = this.outerWidth();
    var stored = [];
    var elem = [];

    var bounds = $this.offset();
    bounds = [bounds.left,bounds.top,bounds.left + width,bounds.top + height];

    i开发者_如何转开发nit();

    function init() {
        $(document).bind('mousedown',scrollCheck)
    }

    function scrollCheck(e) {
        data = $.data($(e.target).get(0),'events')
        if (data && (data.click || data.mousedown)) return false

        if (e.clientX < bounds[0] || e.clientX > bounds[2] ||
            e.clientY < bounds[1] || e.clientY > bounds[3]) return false

        origin = e.clientY;

        $(document).bind('mousemove',function(e) { scrollMove(e) });
        $(document).bind('mouseup',stopScroll);
    }

    function scrollMove(e) {
        current = e.clientY;
        difference = origin - current;

        $this.children().each(function(a,b) {
            if (!stored[a]) {
                stored[a] = $(this).offset().top
                elem.push(b);
            }
            var y = stored[a];

            $(this).offset({top:y - difference});
        });
    }

    function stopScroll(e) {
        $(document).unbind('mousemove').unbind('mouseup');

        if ($(elem[0]).offset().top > 0) resetElem(-$(elem[0]).offset().top + 30)
        else if ($(elem[elem.length-1]).offset().top < height) resetElem(-$(elem[elem.length-1]).offset().top - 50 + height)
        else {
            stored = [];
            elem = [];
        }
    }

    function resetElem(dir) {
        var dist = $(elem[0]).offset().top - 20;
        $this.children().each(function(a,b) {
            if (!stored[a]) stored[a] = $(this)
            var y = $(this).offset().top - dist;

            $(this).animate({
                top: '+=' + dir
            },1000,function() {
                stored = [];
                elem = [];
            });
        });
    }

    return this
};
})(jQuery);
0

精彩评论

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

关注公众号