最近在给一个客户做外贸网店的设计,因为网站展示的内容比较多,所以客户后来要求给添加一个返回顶部的按钮,这样的效果本来不难,只是一个JS问题就能解决,但是因为客户网站上有很多特效,还有其他的客服代码,因此JS之间就容易起冲突,导致开始的几次都以失败告终,经过多次的高度和修改,最终得到了想要的效果(只有鼠标滑动超过一电脑一屏的时候,才会出现出现返回顶部按钮,而且有渐变效果,鼠标滑回一屏以内的位置,返回按钮自动隐藏。)
代码:
var scrolltotop={ //startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control //scrollto: Keyword (Integer, or “Scroll_to_Element_ID”). How far to scroll document up when control is clicked on (0=top). setting: {startline:100, scrollto: 0, scrollduration:1000, fadeduration:[500, 100]}, controlHTML: ‘<img src=”images/top.gif” style=”width:50px; height:51px” />’, //HTML for control, which is auto wrapped in DIV w/ ID=”topcontrol” controlattrs: {offsetx:5, offsety:5}, //offset of control relative to right/ bottom of window corner anchorkeyword: ‘#top’, //Enter href value of HTML anchors on the page that should also act as “Scroll Up” links
state: {isvisible:false, shouldvisible:false},
scrollup:function(){ if (!this.cssfixedsupport) //if control is positioned using JavaScript this.$control.css({opacity:0}) //hide control immediately after clicking it var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto) if (typeof dest==”string” && jQuery(‘#’+dest).length==1) //check element set by string exists 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 //not IE or IE7+ browsers in standards mode 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:’Scroll Back to Top’}) .click(function(){mainobj.scrollup(); return false}) .appendTo(‘body’) if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!=”) //loose check for IE6 and below, plus whether control contains any text mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text mainobj.togglecontrol() $(‘a[href="' + mainobj.anchorkeyword +'"]‘).click(function(){ mainobj.scrollup() return false }) $(window).bind(‘scroll resize’, function(e){ mainobj.togglecontrol() }) }) }}
scrolltotop.init();
纯JS代码,只需将JS里面的图片替换成自己的图片,把JS引用入html代码即可,代码兼容性好!