我正在尝试从headroom.js(http://wicky.nillia.ms/headroom.js/)启动脚本,以便在元素到达页面顶部时触发。 如果调整窗口大小,元素的位置将会改变。
我的问题:函数的偏移值不希望在$(window).resize上使用它。变量offset会更新其值,但函数在定义一次后不会使用它。
这是我的代码:https://jsfiddle.net/kh4jv748/7/
jquery:
//http://wicky.nillia.ms/headroom.js/ is already called
function updateViewportDimensions() {
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
return { width:x,height:y };
}
// setting the viewport width
var viewport = updateViewportDimensions();
$(document).ready(function() {
// initiate the headroom plugin to change nav bar behaviour
function headroom (Offset){
if( typeof Offset === "undefined" ) var Offset = 50;
var $offset = Offset;
console.log('Vertical offset position from "trigger"', Offset);
console.log('function headroom initiated');
$(".top-menu").headroom({
"offset": $offset,
"tolerance": {
"up" : "12", //tolerance scroll back
"down" : "0" // tolerance scroll
},
"classes": {
"initial": "animated",
"pinned": "slideDown",
"unpinned": "slideUp",
"top" : "headroom--top",
"notTop" : "headroom--not-top"
}
});
};
headroom();
$(window).resize(function () {
var home = $('body').hasClass('home');
// if we're on the home page, we wait the set amount (in function above) then fire the function
if( home ) { setTimeout( function() {
// update the viewport, in case the window size has changed
viewport = updateViewportDimensions();
// if we're below 768 fire this off
if( viewport.width < 768 ) {
console.log('Home page < 768.');
var Offset = 0;
} else {
// otherwise, let's do this instead
console.log('Home page > 768');
var Offset = $("#trigger").offset();
var Offset = Offset.top
}
headroom(Offset);
}, 2000, "headroom"); }
else{ setTimeout( function() {
console.log('Not on home page resized');
viewport = updateViewportDimensions();
if( viewport.width < 768 ) {
console.log('Home page < 768.');
var Offset = 0;
console.log(Offset);
} else {
// otherwise, let's do this instead
console.log('Home page > 768');
var Offset = 500;
console.log(Offset);
}
headroom(Offset);
}, 2000, "headroom");
};
});
});
答案 0 :(得分:1)
.headroom('destroy').removeData('headroom')
重新启动该功能并启动一个新的val
$(window).resize(function () {
// if we're on the home page, we wait the set amount (in function above) then fire the function
if( is_home ) { waitForFinalEvent( function() {
// update the viewport, in case the window size has changed
viewport = updateViewportDimensions();
// if we're above or equal to 768 fire this off
if( viewport.width < 768 ) {
//console.log('Home page < 768.');
var Offset = 0;
} else {
// otherwise, let's do this instead
if ($('html #slider-pro-1 .quote').length) { var Offset = $(".widget_image-bubbles-widget").offset();
var Offset = Offset.top - 80; }
else { var Offset = 500; }
}
$(".header").headroom('destroy').removeData('headroom');
headroom(Offset);
}, 1000, "your-function-identifier-string"); }
else{ waitForFinalEvent( function() {
//console.log('Not on home page resized');
viewport = updateViewportDimensions();
if( viewport.width < 768 ) {
var Offset = 0;
} else {
// otherwise, let's do this instead
var Offset = 500;
}
$(".header").headroom('destroy').removeData('headroom');
headroom(Offset);
}, 1000, "your-function-identifier-string");
};
});