anxiao_web/pages/newIndex/js/scroll.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-07-24 09:22:32 +08:00
// JavaScript Document
(function ($) {
$.fn.scroll = function (options) {
//默认配置
var defaults = {
speed: 30, //滚动速度为0-100之间
direction: 'vertical' //方向vertical向上滚动,horizantal向左滚动
};
var opts = $.extend({}, defaults, options), intId = [];
function marquee(obj, step, direction) {
if (direction == 'horizantal') {
obj.find("ul").animate({
marginLeft: '-=1'
}, 0, function () {
var s = Math.abs(parseInt($(this).css("margin-left")));
if (s >= step) {
$(this).find("li").slice(0, 1).appendTo($(this));
$(this).css("margin-left", 0);
}
});
} else if (direction == 'vertical') {
obj.find("ul").animate({
marginTop: '-=1'
}, 0, function () {
var s = Math.abs(parseInt($(this).css("margin-top")));
if (s >= step) {
$(this).find("li").slice(0, 1).appendTo($(this));
$(this).css("margin-top", 0);
}
});
}
}
this.each(function (i) {
var speed = 0 < 100 - opts["speed"] && 100 - opts["speed"] <= 100 ? 100 - opts["speed"] : 30;
var direction = opts["direction"] == 'vertical' || opts["direction"] == 'horizantal' ? opts["direction"] : 'vertical';
var _this = $(this);
intId[i] = setInterval(function () {
var sh;
if (direction == 'horizantal') {
sh = _this.find("ul").find("li:first").outerWidth(true);
} else {
sh = _this.find("ul").find("li:first").outerHeight(true);
}
marquee(_this, sh, direction);
}, speed);
_this.hover(function () {
clearInterval(intId[i]);
}, function () {
intId[i] = setInterval(function () {
var sh;
if (direction == 'horizantal') {
sh = _this.find("ul").find("li:first").outerWidth(true);
} else {
sh = _this.find("ul").find("li:first").outerHeight(true);
}
marquee(_this, sh, direction);
}, speed);
});
});
}
})(jQuery);