基于jquery的回到頁(yè)面頂部按鈕
更新時(shí)間:2011年06月27日 23:56:17 作者:
昨天看到一個(gè)園友的blog中有個(gè)頁(yè)面下滾后出現(xiàn)用于"回到頁(yè)面頂部"按鈕的效果, 感覺(jué)挺不錯(cuò)的, 所以自己也寫了一個(gè), 用jQuery寫是再簡(jiǎn)單不過(guò)了. 下面就直接給出代碼了
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沒(méi)有效果的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, // 超過(guò)100px 顯示按鈕
right:10,
bottom:10,
duration:200, // 回到頁(yè)面頂部的時(shí)間間隔
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沒(méi)有效果的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, // 超過(guò)100px 顯示按鈕
right:10,
bottom:10,
duration:200, // 回到頁(yè)面頂部的時(shí)間間隔
title:'back to top' // div的title屬性
}
})(jQuery);
$.scrollBtn({
showScale: 200, //下滾200px后 顯示按鈕
bottom:20, // 靠底部20px
right:20 // 靠右部20px
});
backToTop.rar
相關(guān)文章
jQuery實(shí)現(xiàn)div跟隨鼠標(biāo)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)div跟隨鼠標(biāo)移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05基于JQuery實(shí)現(xiàn)的Select級(jí)聯(lián)
Select級(jí)聯(lián),想必大并不陌生吧,本文為大家介紹下使用jquery是如何快速實(shí)現(xiàn)的,希望對(duì)大家有所幫助2014-01-01jquery pagination分頁(yè)插件使用詳解(后臺(tái)struts2)
這篇文章主要為大家詳細(xì)介紹了jquery pagination 分頁(yè)插件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01jquery 簡(jiǎn)單圖片導(dǎo)航插件jquery.imgNav.js
前幾天某MM要偶幫忙實(shí)現(xiàn)栗子汀首頁(yè)的圖片導(dǎo)航效果,很簡(jiǎn)單而且具有通用性的一個(gè)需求,點(diǎn)圖片A切換圖片A相關(guān)的內(nèi)容,點(diǎn)圖片B切換圖片B相關(guān)的內(nèi)容,僅此而已。2010-03-03基于jQuery實(shí)現(xiàn)動(dòng)態(tài)數(shù)字展示效果
Jq數(shù)據(jù)列表動(dòng)態(tài)效果,動(dòng)態(tài)更新,支持Ajax動(dòng)態(tài)刷新。下面小編給大家介紹下基于jQuery實(shí)現(xiàn)動(dòng)態(tài)數(shù)字展示效果,需要的朋友可以參考下2015-08-08jquery實(shí)現(xiàn)選項(xiàng)卡切換代碼實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)選項(xiàng)卡切換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05jquery popupDialog 使用 加載jsp頁(yè)面的方法
下面小編就為大家?guī)?lái)一篇jquery popupDialog 使用 加載jsp頁(yè)面的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10