基于jquery的回到頁面頂部按鈕
更新時間:2011年06月27日 23:56:17 作者:
昨天看到一個園友的blog中有個頁面下滾后出現(xiàn)用于"回到頁面頂部"按鈕的效果, 感覺挺不錯的, 所以自己也寫了一個, 用jQuery寫是再簡單不過了. 下面就直接給出代碼了
css代碼:
.scroll-up {
background: #dcdcdc url('up.png') no-repeat center center;
width:48px !important; /*for ff and other standard browser*/
height:48px !important;
_width: 58px; /*for IE6 nonstandard box model*/
_height: 58px;
position: fixed;
_position: absolute; /*for IE6 fixed bug*/
opacity: .6;
filter: Alpha(opacity=60); /*for IE opacity*/
padding:5px;
cursor: pointer;
display: none;
/*css3 property for ff chrome*/
border-radius:5px;
-webkit-transition-property: background-color, opacity;
-webkit-transition-duration: 1s;
-webkit-transition-timing-function: ease;
-moz-transition-property: background-color;
-moz-transition-duration: 1s;
-moz-transition-timing-function: ease;
}
.scroll-up:hover {
background-color:#B9B9B9;
opacity: .8;
}
下面是jquery代碼
;(function($) {
$.scrollBtn = function(options) {
var opts = $.extend({}, $.scrollBtn.defaults, options);
var $scrollBtn = $('<DIV></DIV>').css({
bottom: opts.bottom + 'px',
right: opts.right + 'px'
}).addClass('scroll-up').attr('title', opts.title)
.click(function() {
$('html, body').animate({scrollTop: 0}, opts.duration);
}).appendTo('body');
// 綁定到window的scroll事件
$(window).bind('scroll', function() {
var scrollTop = $(document).scrollTop(),
viewHeight = $(window).height();
// 小于配置的顯示范圍 則fadeOut
if(scrollTop <= opts.showScale) {
if($scrollBtn.is(':visible'))
$scrollBtn.fadeOut(500);
// 大于配置的顯示范圍 則fadeIn
} else {
if($scrollBtn.is(':hidden'))
$scrollBtn.fadeIn(500);
}
// 解決IE6下css中fixed沒有效果的bug
if(isIE6()) {
var top = viewHeight + scrollTop - $scrollBtn.outerHeight() - opts.bottom;
$scrollBtn.css('top', top + 'px');
}
});
// 判斷是否為IE6
function isIE6() {
if($.browser.msie) {
if($.browser.version == '6.0') return true;
}
}
};
/**
* -params
* -showScale: scroll down how much to show the scrollup button
* -right: to right of scrollable container
* -bottom: to bottom of scrollable container
*/
$.scrollBtn.defaults = { // 默認(rèn)值
showScale: 100, // 超過100px 顯示按鈕
right:10,
bottom:10,
duration:200, // 回到頁面頂部的時間間隔
title:'back to top' // div的title屬性
}
})(jQuery);
$.scrollBtn({
showScale: 200, //下滾200px后 顯示按鈕
bottom:20, // 靠底部20px
right:20 // 靠右部20px
});
backToTop.rar
復(fù)制代碼 代碼如下:
.scroll-up {
background: #dcdcdc url('up.png') no-repeat center center;
width:48px !important; /*for ff and other standard browser*/
height:48px !important;
_width: 58px; /*for IE6 nonstandard box model*/
_height: 58px;
position: fixed;
_position: absolute; /*for IE6 fixed bug*/
opacity: .6;
filter: Alpha(opacity=60); /*for IE opacity*/
padding:5px;
cursor: pointer;
display: none;
/*css3 property for ff chrome*/
border-radius:5px;
-webkit-transition-property: background-color, opacity;
-webkit-transition-duration: 1s;
-webkit-transition-timing-function: ease;
-moz-transition-property: background-color;
-moz-transition-duration: 1s;
-moz-transition-timing-function: ease;
}
.scroll-up:hover {
background-color:#B9B9B9;
opacity: .8;
}
下面是jquery代碼
復(fù)制代碼 代碼如下:
;(function($) {
$.scrollBtn = function(options) {
var opts = $.extend({}, $.scrollBtn.defaults, options);
var $scrollBtn = $('<DIV></DIV>').css({
bottom: opts.bottom + 'px',
right: opts.right + 'px'
}).addClass('scroll-up').attr('title', opts.title)
.click(function() {
$('html, body').animate({scrollTop: 0}, opts.duration);
}).appendTo('body');
// 綁定到window的scroll事件
$(window).bind('scroll', function() {
var scrollTop = $(document).scrollTop(),
viewHeight = $(window).height();
// 小于配置的顯示范圍 則fadeOut
if(scrollTop <= opts.showScale) {
if($scrollBtn.is(':visible'))
$scrollBtn.fadeOut(500);
// 大于配置的顯示范圍 則fadeIn
} else {
if($scrollBtn.is(':hidden'))
$scrollBtn.fadeIn(500);
}
// 解決IE6下css中fixed沒有效果的bug
if(isIE6()) {
var top = viewHeight + scrollTop - $scrollBtn.outerHeight() - opts.bottom;
$scrollBtn.css('top', top + 'px');
}
});
// 判斷是否為IE6
function isIE6() {
if($.browser.msie) {
if($.browser.version == '6.0') return true;
}
}
};
/**
* -params
* -showScale: scroll down how much to show the scrollup button
* -right: to right of scrollable container
* -bottom: to bottom of scrollable container
*/
$.scrollBtn.defaults = { // 默認(rèn)值
showScale: 100, // 超過100px 顯示按鈕
right:10,
bottom:10,
duration:200, // 回到頁面頂部的時間間隔
title:'back to top' // div的title屬性
}
})(jQuery);
$.scrollBtn({
showScale: 200, //下滾200px后 顯示按鈕
bottom:20, // 靠底部20px
right:20 // 靠右部20px
});
backToTop.rar
相關(guān)文章
基于JQuery實現(xiàn)的Select級聯(lián)
Select級聯(lián),想必大并不陌生吧,本文為大家介紹下使用jquery是如何快速實現(xiàn)的,希望對大家有所幫助2014-01-01jquery pagination分頁插件使用詳解(后臺struts2)
這篇文章主要為大家詳細(xì)介紹了jquery pagination 分頁插件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01jquery 簡單圖片導(dǎo)航插件jquery.imgNav.js
前幾天某MM要偶幫忙實現(xiàn)栗子汀首頁的圖片導(dǎo)航效果,很簡單而且具有通用性的一個需求,點圖片A切換圖片A相關(guān)的內(nèi)容,點圖片B切換圖片B相關(guān)的內(nèi)容,僅此而已。2010-03-03基于jQuery實現(xiàn)動態(tài)數(shù)字展示效果
Jq數(shù)據(jù)列表動態(tài)效果,動態(tài)更新,支持Ajax動態(tài)刷新。下面小編給大家介紹下基于jQuery實現(xiàn)動態(tài)數(shù)字展示效果,需要的朋友可以參考下2015-08-08jquery popupDialog 使用 加載jsp頁面的方法
下面小編就為大家?guī)硪黄猨query popupDialog 使用 加載jsp頁面的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10