Skip to content

Instantly share code, notes, and snippets.

@sivagao
Last active December 15, 2015 21:50
Show Gist options
  • Select an option

  • Save sivagao/5328854 to your computer and use it in GitHub Desktop.

Select an option

Save sivagao/5328854 to your computer and use it in GitHub Desktop.
JS: goTop(smooth move)
$(document).ready(function() {
$('.goto-top').click(function(event) {
event.preventDefault();
move(documentScrollTop());
//window.scrollTo(0, 0);
});
});
//获取或设置文档对象的scrollTop
//function([val])
function documentScrollTop(val){
var elem = document;
return (val!== undefined) ?
elem.documentElement.scrollTop = elem.body.scrollTop = val :
Math.max(elem.documentElement.scrollTop, elem.body.scrollTop);
}
//减速移动滚动条
function move(scrollTop){
setTimeout(function(){
scrollTop = Math.floor((scrollTop * 0.65));
documentScrollTop(scrollTop);
if(scrollTop !=0 ){
setTimeout(arguments.callee, 25);
}
}, 25);
}
// 滚动时候, 是否显示gotoTop
function scrollHandler(event) {
var $gotoTop = $('.goto-top');
if ($(window).scrollTop() > 0) {
if (!visible) {
if (isIE6) {
if ($gotoTop.css('position') != 'absolute') {
$gotoTop.css({
position: 'absolute'
});
$gotoTop[0].style.setExpression("top", "eval(document.documentElement.scrollTop + documentElement.clientHeight - this.clientHeight - 40) + 'px'");
}
}
else {
if ($gotoTop.css('position') != 'fixed') {
$gotoTop.css({position: 'fixed'});
}
}
visible = true;
isIE6 ? $gotoTop.show() : $gotoTop.fadeIn('slow');
}
}
else {
if (visible) {
visible = false;
if(isIE6){
$gotoTop.css({position: 'relative'});
$gotoTop.hide();
}
else{
$gotoTop.fadeOut('fast',function(){
$gotoTop.css({position: 'relative'});
});
}
//isIE6 ? $gotoTop.hide() : $gotoTop.fadeOut('slow');
}
}
}
$(window).scroll(Utils.buffer(scrollHandler, 200));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment