Skip to content

Instantly share code, notes, and snippets.

@subchen
Last active December 28, 2015 02:09
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 subchen/7426265 to your computer and use it in GitHub Desktop.
Save subchen/7426265 to your computer and use it in GitHub Desktop.
返回到顶部效果 for Javascript
/******************************************
* jetbrick.widget.scrolltop.js
******************************************/
(function (window, $) {
/**
* 单击此处,返回到顶部
*/
var scrolltop = function() {
var $dom = $('img[jw-scrolltop]');
if ($dom.length == 0) {
$dom = $('<img src="/assets/images/scrolltop.png" />');
$('body').append($dom);
}
$dom.css({
display: 'none',
cursor: 'pointer',
position: 'fixed',
right: '60px',
bottom: '80px',
border: '0 none',
margin: 0,
padding: 0,
width: 'auto',
height: 'auto'
});
$dom.click(function() {
$('body,html').animate({scrollTop:0}, 400);
return false;
});
var hidden = true;
$(window).on('scroll resize', function() {
if ($(window).scrollTop() > 40) {
if (hidden) {
$dom.fadeIn();
hidden = false;
}
} else {
if (!hidden) {
$dom.fadeOut();
hidden = true;
}
}
});
};
$(function() {
scrolltop();
});
})(typeof window !== "undefined" ? window : this, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment