Javascript 当页面完全滚动到顶部时隐藏“转到顶部”按钮
我的wordpress主题主页上有一个按钮,用于转到页面顶部。我想在页面完全滚动到顶部时隐藏它。这是我的密码:Javascript 当页面完全滚动到顶部时隐藏“转到顶部”按钮,javascript,jquery,scroll,Javascript,Jquery,Scroll,我的wordpress主题主页上有一个按钮,用于转到页面顶部。我想在页面完全滚动到顶部时隐藏它。这是我的密码: (function($) { var version = '@VERSION', defaults = { exclude: [], excludeWithin:[], offset: 0, direction: 'top', // one of 'top' or 'left' scrollEleme
(function($) {
var version = '@VERSION',
defaults = {
exclude: [],
excludeWithin:[],
offset: 0,
direction: 'top', // one of 'top' or 'left'
scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll).
// if null (default), $('html, body').firstScrollable() is used.
scrollTarget: null, // only use if you want to override default behavior
beforeScroll: function() {}, // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled
afterScroll: function() {}, // fn(opts) function to be called after scrolling occurs. "this" is the triggering element
easing: 'swing',
speed: 600,
autoCoefficent: 2 // coefficient for "auto" speed
},
getScrollable = function(opts) {
var scrollable = [],
scrolled = false,
dir = opts.dir && opts.dir == 'left' ? 'scrollLeft' : 'scrollTop';
this.each(function() {
if (this == document || this == window) { return; }
var el = $(this);
if ( el[dir]() > 0 ) {
scrollable.push(this);
} else {
// if scroll(Top|Left) === 0, nudge the element 1px and see if it moves
el[dir](1);
scrolled = el[dir]() > 0;
if ( scrolled ) {
scrollable.push(this);
}
// then put it back, of course
el[dir](0);
}
});
// If no scrollable elements, fall back to <body>,
// if it's in the jQuery collection
// (doing this because Safari sets scrollTop async,
// so can't set it to 1 and immediately get the value.)
if (!scrollable.length) {
this.each(function(index) {
if (this.nodeName === 'BODY') {
scrollable = [this];
}
});
}
// Use the first scrollable element if we're calling firstScrollable()
if ( opts.el === 'first' && scrollable.length > 1 ) {
scrollable = [ scrollable[0] ];
}
return scrollable;
},
isTouch = 'ontouchend' in document;
$.fn.extend({
scrollable: function(dir) {
var scrl = getScrollable.call(this, {dir: dir});
return this.pushStack(scrl);
},
firstScrollable: function(dir) {
var scrl = getScrollable.call(this, {el: 'first', dir: dir});
return this.pushStack(scrl);
},
smoothScroll: function(options) {
options = options || {};
var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
locationPath = $.smoothScroll.filterPath(location.pathname);
this
.unbind('click.smoothscroll')
.bind('click.smoothscroll', function(event) {
var link = this,
$link = $(this),
exclude = opts.exclude,
excludeWithin = opts.excludeWithin,
elCounter = 0, ewlCounter = 0,
include = true,
clickOpts = {},
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath,
thisHash = escapeSelector(link.hash);
if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
include = false;
} else {
while (include && elCounter < exclude.length) {
if ($link.is(escapeSelector(exclude[elCounter++]))) {
include = false;
}
}
while ( include && ewlCounter < excludeWithin.length ) {
if ($link.closest(excludeWithin[ewlCounter++]).length) {
include = false;
}
}
}
if ( include ) {
event.preventDefault();
$.extend( clickOpts, opts, {
scrollTarget: opts.scrollTarget || thisHash,
link: link
});
$.smoothScroll( clickOpts );
}
});
return this;
}
});
$.smoothScroll = function(options, px) {
var opts, $scroller, scrollTargetOffset, speed,
scrollerOffset = 0,
offPos = 'offset',
scrollDir = 'scrollTop',
aniProps = {},
aniOpts = {},
scrollprops = [];
if ( typeof options === 'number') {
opts = $.fn.smoothScroll.defaults;
scrollTargetOffset = options;
} else {
opts = $.extend({}, $.fn.smoothScroll.defaults, options || {});
if (opts.scrollElement) {
offPos = 'position';
if (opts.scrollElement.css('position') == 'static') {
opts.scrollElement.css('position', 'relative');
}
}
scrollTargetOffset = px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
0;
}
opts = $.extend({link: null}, opts);
scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir;
if ( opts.scrollElement ) {
$scroller = opts.scrollElement;
scrollerOffset = $scroller[scrollDir]();
} else {
$scroller = $('html, body').firstScrollable();
}
aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
opts.beforeScroll.call($scroller, opts);
speed = opts.speed;
// automatically calculate the speed of the scroll based on distance / coefficient
if (speed === 'auto') {
// if aniProps[scrollDir] == 0 then we'll use scrollTop() value instead
speed = aniProps[scrollDir] || $scroller.scrollTop();
// divide the speed by the coefficient
speed = speed / opts.autoCoefficent;
}
aniOpts = {
duration: speed,
easing: opts.easing,
complete: function() {
opts.afterScroll.call(opts.link, opts);
}
};
if (opts.step) {
aniOpts.step = opts.step;
}
if ($scroller.length) {
$scroller.stop().animate(aniProps, aniOpts);
} else {
opts.afterScroll.call(opts.link, opts);
}
};
$.smoothScroll.version = version;
$.smoothScroll.filterPath = function(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
};
// default options
$.fn.smoothScroll.defaults = defaults;
function escapeSelector (str) {
return str.replace(/(:|\.)/g,'\\$1');
}
})(jQuery);
(函数($){
var version='@version',
默认值={
不包括:[],
不包括在[]之内,
偏移量:0,
方向:'顶部',//顶部或左侧之一
scrollElement:null,//jQuery要滚动的元素集(对于$.smoothScroll)。
//如果为null(默认值),则使用$('html,body').firstScrollable()。
scrollTarget:null,//仅当您要覆盖默认行为时使用
beforecroll:function(){},//fn(opts)在滚动发生之前调用的函数。“this”是正在滚动的元素
afterScroll:function(){},//fn(opts)在滚动发生后调用的函数。“this”是触发元素
放松:"摇摆",,
速度:600,,
自动效率:2//“自动”速度系数
},
getScrollable=函数(opts){
var scrollable=[],
滚动=错误,
dir=opts.dir&&opts.dir=='left'?'scrollLeft':'scrollTop';
这个。每个(函数(){
如果(this==document | | this==window){return;}
var el=$(本);
如果(el[dir]()>0){
可滚动。按(此);
}否则{
//如果滚动(左上方)==0,则轻推元素1px并查看其是否移动
el[dir](1);
滚动=el[dir]()>0;
如果(滚动){
可滚动。按(此);
}
//当然,把它放回去
el[dir](0);
}
});
//如果没有可滚动的元素,请返回,
//如果它在jQuery集合中
//(这样做是因为Safari将scrollTop设置为异步,
//因此无法将其设置为1并立即获取值。)
如果(!scrollable.length){
此。每个(函数(索引){
if(this.nodeName==='BODY'){
可滚动=[this];
}
});
}
//如果我们调用firstScrollable(),请使用第一个可滚动元素
if(opts.el=='first'&&scrollable.length>1){
可滚动=[可滚动[0]];
}
返回可滚动;
},
isTouch='ontouchend'在文档中;
$.fn.extend({
可滚动:功能(目录){
var scrl=getScrollable.call(这个,{dir:dir});
返回此.pushStack(scrl);
},
第一个可滚动:函数(dir){
var scrl=getScrollable.call(这个,{el:'first',dir:dir});
返回此.pushStack(scrl);
},
平滑滚动:功能(选项){
选项=选项| |{};
var opts=$.extend({},$.fn.smoothScroll.defaults,options),
locationPath=$.smoothScroll.filterPath(location.pathname);
这
.unbind('click.smoothscroll')
.bind('click.smoothscroll',函数(事件){
var link=这个,
$link=$(此),
排除=选择排除,
excludeintern=opts.excludeintern,
elCounter=0,EWLCUNTER=0,
include=true,
单击选项={},
hostMatch=((location.hostname==link.hostname)| |!link.hostname),
pathMatch=opts.scrollTarget | |($.smoothScroll.filterPath(link.pathname)| | locationPath)==locationPath,
thisHash=转义选择器(link.hash);
如果(!opts.scrollTarget&&(!hostMatch | | |!pathMatch | |!thisHash)){
include=false;
}否则{
while(include&&elCounter $(window).bind( "mousewheel DOMMouseScroll scroll", function(e){
if (document.body.scrollTop == 0) {
// do this
}
})