网页内容实现太长实现返回顶部的代码

1、

<a id="gotopbtn" style="position:fixed;width:12px;bottom:45px;right:150px;display:none;cursor:pointer;">返回顶部</a>

<script>

var backTop = function(btnId){

var btn = document.getElementById(btnId);

var d = document.documentElement || document.body;

window.onscroll = set;

btn.onclick = function () {

btn.style.display = "none";

window.onscroll = null;

this.timer = setInterval(function () {

//console.log(document.body.scrollTop)

//console.log(document.documentElement.scrollTop)

if( document.body.scrollTop ){

document.body.scrollTop -= Math.ceil(document.body.scrollTop * 0.1);

}else{

document.documentElement.scrollTop -= Math.ceil(document.documentElement.scrollTop * 0.1);

}

if (document.body.scrollTop == 1) clearInterval(btn.timer), window.onscroll = set, document.body.scrollTop = 0;

if (document.documentElement.scrollTop == 1) clearInterval(btn.timer), window.onscroll = set, document.documentElement.scrollTop = 0;

}, 10);

};

function set() { btn.style.display = (document.documentElement.scrollTop || document.body.scrollTop) ? ‘block‘ : "none" }

};

backTop(‘gotopbtn‘);

</script>

将上面代码直接复制到最后的</body>标签之上就行了

2、style.css

/*gotop*/

.cbbfixed {position: fixed;right: 10px;transition: bottom ease .3s;bottom: -85px;z-index: 3;cursor:pointer;}

.cbbfixed .cbbtn {width: 40px;height: 40px;display: block;background-color: #02a2aa;}

.cbbfixed .gotop {transition: background-color ease .3s;margin-top: 1px;}

.cbbfixed .gotop .up-icon{float:left;margin:14px 0 0 9px;width:23px;height:12px;background: url(../images/side-icon02.png);}

.cbbfixed .gotop:hover {background-color:#2c2d2e;}

.cbbfixed .cweixin {background: #dadbdd;}

.cbbfixed .cweixin div {background: url(../images/erwei.png);width: 194px;height: 199px;position: absolute;left: -194px;top: -115px;opacity: 0;transform: scale(0);    -webkit-transform: scale(0);transform-origin: 100% 70%;-webkit-transform-origin: 100% 70%;transition:
transform ease .3s,opacity ease .3s;-webkit-transition: all ease .3s;display:none\0;*display:none;}

.cbbfixed .cweixin:hover div {display: block;transform: scale(1);-webkit-transform: scale(1);opacity: 1;*display:block;}

.cbbfixed .cweixin .weixin-icon{float: left;margin:12px 0 0 11px;width:18px;height: 18px; background: url(../images/side-icon01.png);}

图片什么的自己对应着改就行了

引入jquery.js

引入backtop.js

backtop.js

//javascript Document

function chinaz(){

this.init();

}

chinaz.prototype = {

constructor: chinaz,

init: function(){

this._initBackTop();

},

_initBackTop: function(){

var $backTop = this.$backTop = $(‘<div class="cbbfixed">‘+

‘<a class="cweixin cbbtn"">‘+

‘<span class="weixin-icon"></span><div></div>‘+

‘</a>‘+

‘<a class="gotop cbbtn">‘+

‘<span class="up-icon"></span>‘+

‘</a>‘+

‘</div>‘);

$(‘body‘).append($backTop);

$backTop.click(function(){

$("html, body").animate({

scrollTop: 0

}, 120);

});

var timmer = null;

$(window).bind("scroll",function() {

var d = $(document).scrollTop(),

e = $(window).height();

0 < d ? $backTop.css("bottom", "10px") : $backTop.css("bottom", "-90px");

clearTimeout(timmer);

timmer = setTimeout(function() {

clearTimeout(timmer)

},100);

});

}

}

var chinaz = new chinaz();

实例代码http://download.csdn.net/detail/wangdianyong/8583979

3、style样式

<style>

.up{width:54px;height:54px;background:url(images/topback.gif) no-repeat 0 0;filter:alpha(Opacity=60);-moz-opacity:0.6;opacity: 0.6;}.up:hover{filter:alpha(Opacity=100);-moz-opacity:1;opacity:1;}

</style>

引入jquery

引入scrolltopcontrol.js

scrolltopcontrol.js

var scrolltotop={

setting:{

startline:100, //起始行

scrollto:0, //滚动到指定位置

scrollduration:400, //滚动过渡时间

fadeduration:[500,100] //淡出淡现消失

},

controlHTML:‘<div class="up"></div>‘, //返回顶部按钮

controlattrs:{offsetx:30,offsety:80},//返回按钮固定位置

anchorkeyword:"#top",

state:{

isvisible:false,

shouldvisible:false

},scrollup:function(){

if(!this.cssfixedsupport){

this.$control.css({opacity:0});

}

var dest=isNaN(this.setting.scrollto)?this.setting.scrollto:parseInt(this.setting.scrollto);

if(typeof dest=="string"&&jQuery("#"+dest).length==1){

dest=jQuery("#"+dest).offset().top;

}else{

dest=0;

}

this.$body.animate({scrollTop:dest},this.setting.scrollduration);

},keepfixed:function(){

var $window=jQuery(window);

var controlx=$window.scrollLeft()+$window.width()-this.$control.width()-this.controlattrs.offsetx;

var controly=$window.scrollTop()+$window.height()-this.$control.height()-this.controlattrs.offsety;

this.$control.css({left:controlx+"px",top:controly+"px"});

},togglecontrol:function(){

var scrolltop=jQuery(window).scrollTop();

if(!this.cssfixedsupport){

this.keepfixed();

}

this.state.shouldvisible=(scrolltop>=this.setting.startline)?true:false;

if(this.state.shouldvisible&&!this.state.isvisible){

this.$control.stop().animate({opacity:1},this.setting.fadeduration[0]);

this.state.isvisible=true;

}else{

if(this.state.shouldvisible==false&&this.state.isvisible){

this.$control.stop().animate({opacity:0},this.setting.fadeduration[1]);

this.state.isvisible=false;

}

}

},init:function(){

jQuery(document).ready(function($){

var mainobj=scrolltotop;

var iebrws=document.all;

mainobj.cssfixedsupport=!iebrws||iebrws&&document.compatMode=="CSS1Compat"&&window.XMLHttpRequest;

mainobj.$body=(window.opera)?(document.compatMode=="CSS1Compat"?$("html"):$("body")):$("html,body");

mainobj.$control=$(‘<div id="topcontrol">‘+mainobj.controlHTML+"</div>").css({position:mainobj.cssfixedsupport?"fixed":"absolute",bottom:mainobj.controlattrs.offsety,right:mainobj.controlattrs.offsetx,opacity:0,cursor:"pointer"}).attr({title:"返回顶部"}).click(function(){mainobj.scrollup();return
false;}).appendTo("body");if(document.all&&!window.XMLHttpRequest&&mainobj.$control.text()!=""){mainobj.$control.css({width:mainobj.$control.width()});}mainobj.togglecontrol();

$(‘a[href="‘+mainobj.anchorkeyword+‘"]‘).click(function(){mainobj.scrollup();return false;});

$(window).bind("scroll resize",function(e){mainobj.togglecontrol();});

});

}

};

scrolltotop.init();

修改相应路径的图片即可。

实例代码http://download.csdn.net/detail/wangdianyong/8584001

时间: 2024-08-27 06:19:26

网页内容实现太长实现返回顶部的代码的相关文章

一个简单的返回顶部jQuery代码

本文演示jQuery返回顶部,当文章页比较长时间,可以方便用户返回网站的顶部,实现代码也不难,大家学习下! jQuery返回顶部 调用代码: <script language="javascript" type="text/javascript" src="jquery-1.4.2.min.js"></script> <script language="javascript" type="

弹性返回顶部JS代码

弹性返回顶部JS代码 弹性返回顶部JS代码点击下载

手机端网页返回顶部js代码

<!DOCTYPE html>  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>手机端网页返回顶部js代码</title>  <script src="http://libs.baidu.com/jquery/1.11.3/jquery.m

多个返回 顶部的代码

代码1: $("#back-to-top").click(function () { $('body,html').animate({ scrollTop: 0 }, 1000); return false; }); 代码2: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title&

20款网页常用的返回顶部代码

jQuery网页滚动显示浮动导航带返回顶部按钮 css3悬浮返回顶部按钮悬停显示二维码特效 jQuery网页返回顶部固定层微信二维码代码 jQuery绿色的qq在线客服点击返回顶部代码 div css网页右侧返回顶部样式代码 jQuery响应式网页返回顶部按钮代码 jQuery win8扁平风格返回顶部和在线客服网站侧边栏代码 jquery扁平风格带二维码的页面滑动返回顶部按钮代码 jQuery仿威锋商城网站右侧悬浮层返回顶部代码 jQuery带微信二维码网页侧边返回顶部效果 jquery wi

我要返回顶部

我觉得一个网站最不可少的小功能就是返回顶部了吧,尤其是那些长的要命,拉都拉不到尽头的,简直丧心病狂,如果没有返回顶部的按钮的话,鬼愿意原路拉回去,直接关掉走人.所以一个返回顶部的按钮还是需要的,当然,返回顶部也可以做出很多花样,本文只讨论如何返回,不管具体的外观样式,那都是后话. 以下是一些方法: 1. <a href='#'>返回顶部</a> ok,搞定,是不是觉得很坑爹,但我刚开始接触的时候就是用这个方法的,简单暴力直接,就是硬生生的直接跳回去,不好看,没有美感,而且直接这样不

原生JS返回顶部,带返回效果

有些网站当滑到一定高度时右下角会有一个按钮,你只要一点就可以直接返回顶部了.那这个功能是怎么做到的呢.其实不算太难: 首先我们先在网页中创建一个按钮,上面写上返回顶部,把它的样式改成固定定位,之后想要什么样式都可以自己写上,再在上面设置一个id: 之后我们js中获取一下这个按钮的id,并且定义一个变量timer,方便接下来清除间隔器: 之后我们设置一个页面的滚动事件,在里面定义一个变量获取一下所滚动的高度: 再用一个判断语句来判断一下滚动的高度:我们在设置一个数来让按钮什么时候显示: var o

js返回顶部效果

当用户浏览的网页过于长的时候,用户在浏览到网页底部想要在返回顶部需要滚动好几次滚轮才能返回顶部,不仅麻烦,而且用户体验也会很差.现在的大多是页面都会在页面顶部或者是页面的可见区域的某一位置固定一个按钮,点击它可以使页面返回顶部,用户再也不用滚动滚轮了.下面我总结了集中常用的返回顶部的效果: 方法一(最简单,代码量最少,但是效果有些生硬).代码如下: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <

简单返回顶部代码及注释说明

最近做了一个排行榜页面,分类较多,并且以图文形式展示......最后页面明显过长了,用户体验自然不够好.最后想着在页面上部增加了页面内跳转导航,直接导航到本页的分类命名锚块.当然这就需要结合“返回顶部”功能来取得更好的体验了.当然一般情况下首先还是要避免页面过长,内容薄弱关联性不够的跳出率会很高. 下面就说下简单的返回顶部效果的代码实现,附注释说明. 1. 最简单的静态返回顶部,点击直接跳转页面顶部,常见于固定放置在页面底部返回顶部功能 方法一:用命名锚点击返回到顶部预设的id为top的元素 h