Responsive JavaScript – load/hide specific function related to browser width?

冷暖自知 提交于 2019-12-11 21:58:49

问题


first fo all: i'm not into JavaScript/JQuery! …but i will try to point out what i want to do.

i'm using the fantastic bx-slider on my page and i made my website responsive via media queries which both works great so far. …but there are some features from the bx-slider-script which i don't want to get active on a specific browser-width. here's what i want:

browser-width LOWER than 500px = load this function:

$('#slider4').bxSlider({
    minSlides: 2,
    maxSlides: 2,
    slideWidth: 230,
    slideMargin: 10,
    nextSelector: '#next',
    prevSelector: '#prev',
    infiniteLoop: false,
    hideControlOnEnd: true,
    pager: false,
    useCSS: false,
    easing: 'easeInOutExpo',
    speed: 1000
});

browser-width HIGHER than 500px = hide the function above and load this one:

$('#slider4').bxSlider({
    minSlides: 4,
    maxSlides: 4,
    slideWidth: 230,
    slideMargin: 20,
    nextSelector: '#next',
    prevSelector: '#prev',
    infiniteLoop: false,
    hideControlOnEnd: true,
    pager: false,
    useCSS: false,
    easing: 'easeInOutExpo',
    speed: 1000
});

…is that even possible in JavaScript? I tried some scripts which are using breaking points but it think my JS-knowledge is to weak to get this to work as i want.

It would be great if there's someone out here which can show me how to do this easily!


回答1:


If($(document).width() < 500){
...
}else{
...
}

http://api.jquery.com/width/

You may also want to put in an onchange event

$( window ).resize(function() {
 resizefunction();
});

http://api.jquery.com/resize/



来源:https://stackoverflow.com/questions/19422264/responsive-javascript-load-hide-specific-function-related-to-browser-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!