Dus*_*tin 67 javascript jquery responsive-design
我的页面上有一个滚动元素(使用jScrollPane jQuery插件).我想要实现的是通过检测浏览器窗口的宽度来关闭滚动窗口的方法.我正在做一个响应式布局,我希望在浏览器低于一定宽度时关闭此滚动功能.当我刷新页面时,我能够使它工作,但是当我调整浏览器窗口的大小时,宽度值不会立即更新.
现在,如果我开始使用宽度为1000px的窗口,则调整大小为350px,滚动功能仍然存在.一旦浏览器宽度达到440px,我希望滚动功能关闭.
这是我到目前为止的代码..
var windowsize = $(window).width();
$(window).resize(function() {
var windowsize = $(window).width();
});
if (windowsize > 440) {
//if the window is greater than 440px wide then turn on jScrollPane..
$('#pane1').jScrollPane({
scrollbarWidth:15,
scrollbarMargin:52
});
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*b W 146
更改变量不会神奇地执行if
-block中的代码.将公共代码放在一个函数中,然后绑定事件,并调用该函数:
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
var $pane = $('#pane1');
function checkWidth() {
var windowsize = $window.width();
if (windowsize > 440) {
//if the window is greater than 440px wide then turn on jScrollPane..
$pane.jScrollPane({
scrollbarWidth:15,
scrollbarMargin:52
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
Run Code Online (Sandbox Code Playgroud)
ant*_*rat 17
把你的if条件放在resize
函数中:
var windowsize = $(window).width();
$(window).resize(function() {
windowsize = $(window).width();
if (windowsize > 440) {
//if the window is greater than 440px wide then turn on jScrollPane..
$('#pane1').jScrollPane({
scrollbarWidth:15,
scrollbarMargin:52
});
}
});
Run Code Online (Sandbox Code Playgroud)