Last active
October 26, 2016 02:22
-
-
Save think2011/315092260b7b9ced7d48ec658f24751d to your computer and use it in GitHub Desktop.
marquee.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.marquee = function (options) { | |
var $that = this | |
var that = this[0] | |
if (!that) return | |
options = $.extend({}, { | |
speed: 50 | |
}, options) | |
var timer = setInterval(marquee, options.speed) | |
$that | |
.on('touchstart', function () { | |
clearInterval(timer) | |
}) | |
.on('touchend', function () { | |
timer = setInterval(marquee, options.speed) | |
}) | |
function marquee() { | |
var offset = that.scrollHeight - that.offsetHeight | |
var $firstItem = $that.children().first() | |
var firstItemHeight = parseInt(getComputedStyle($firstItem[0]).height) | |
if (offset === 0) { | |
return | |
} | |
// 不足于滚动补充到足够 | |
else if (offset < firstItemHeight) { | |
$that.append($('<div class="clone"></div>').css('height', offset)) | |
} | |
if (that.scrollTop >= firstItemHeight) { | |
$firstItem.appendTo($that) | |
that.scrollTop = 0 | |
} else { | |
that.scrollTop++ | |
} | |
} | |
} | |
$(function () { | |
$('[data-marquee]').marquee() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment