Skip to content

Instantly share code, notes, and snippets.

@livem
Created April 11, 2013 07:12
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 livem/5361362 to your computer and use it in GitHub Desktop.
Save livem/5361362 to your computer and use it in GitHub Desktop.
"返回顶部"插件
@CHARSET "UTF-8";
.backToTop {
width: 100px;
z-index: 10;
border: 1px solid #333;
background: #121212;
text-align: center;
padding: 5px;
position: fixed;
bottom: 0px;
right: 0px;
cursor: pointer;
display: none;
color: #fff;
text-transform: lowercase;
font-size: 0.9em;
}
/**
* jQuery BackToTop plugin 返回顶部
*
* 2013-01-23
*
* @require jQuery
* @require backToTop.css
* @link 参考自 http://www.cnblogs.com/lanmiao/articles/2248053.html
*
* eg: $('html').createBackToTop();
* 或 $('body').createBackToTop();
* 或 $( window ).createBackToTop();
*
*/
;(function( win ){
var _win = win, _doc = _win.document, $ = _win.jQuery;
$.fn.createBackToTop = function() {
var scrollDiv = _doc.createElement('div');
$(scrollDiv).addClass('backToTop').html( '\u8FD4\u56DE\u9876\u90E8' ).appendTo('body'); // window.escape('返回顶部')
$( _win ).scroll(function() {
if ($(this).scrollTop() != 0) {
$('.backToTop').fadeIn();
} else {
$('.backToTop').fadeOut();
}
});
$('.backToTop').click(function() {
$('body,html').animate({ scrollTop: 0 }, 800);
});
};
/**
* 判断当前元素是否存在
*
* @return {Boolean} true 代表存在
*/
$.fn.isExist = function() {
return ( $(this).length > 0 );
};
})( window );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment