当浏览器窗口缩小到一定大小并对列表应用不同的样式时,我试图删除bxslider元素的样式和 javascript,但它正在做奇怪的事情。
从 502px 以下放大,一切看起来和工作正常,但是当再次缩小时,滑块会消失,但新的样式被破坏了。目前我只是重新加载页面,如果它低于 502 像素,这感觉很糟糕。
这是我的代码:
编辑:根据要求,我已将相关的 HTML、javascript 和 CSS 添加到 JSFiddle:http: //jsfiddle.net/tYRJ2/2/。(CSS 不完整,但它显示了问题。)
var sliderActive = false;
var slider;
function createSlider() {
slider = jQuery('.bxslider').bxSlider({
adaptiveHeight: false,
swipeThreshold: 40,
controls: true,
auto: true,
pause: 6000,
autoHover: true,
});
return true;
}
//create slider if new page is wide
jQuery(document).ready(function(){
if (window.innerWidth > 502) {
sliderActive = createSlider();
}
});
//create/destroy slider based on width
jQuery(window).resize(function () {
//if wide and no slider, create slider
if (window.innerWidth > 502 && sliderActive == false) {
sliderActive = createSlider();
}
//else if narrow and active slider, destroy slider
else if (window.innerWidth <= 502 && sliderActive == true){
slider.destroySlider();
sliderActive = false;
location.reload(); //fudgey
}
//else if wide and active slider, or narrow and no slider, do nothing
});
任何帮助深表感谢!