Skip to content

Instantly share code, notes, and snippets.

@think2011
Last active October 26, 2016 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think2011/315092260b7b9ced7d48ec658f24751d to your computer and use it in GitHub Desktop.
Save think2011/315092260b7b9ced7d48ec658f24751d to your computer and use it in GitHub Desktop.
marquee.js
$.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