当滑块只有一个图像时,如何禁用next / prev按钮?

时间:2022-08-23 22:44:30

When slider have one image then don't want next/prev button but if slider have more than 1 images then required next/prev button. Can you please suggest me JavaScript code to do. I know how to disable next/prev through controls false

如果滑块有一个图像,则不需要next / prev按钮,但如果滑块有多个图像,则需要next / prev按钮。你能建议我用JavaScript代码吗?我知道如何禁用next / prev through controls false

controls : false

but it is also false when image have more than one image. Here is my code,

但是当图像有多个图像时,它也是错误的。这是我的代码,

<script>
var imgCnt = $('#images div').val(); //get images count.
if(imgCnt == 1){
$('.bxslider').bxSlider({
controls:false
});
}else{
controls:true
}
</script>

2 个解决方案

#1


0  

I have tried this code and it's working fine. Please try to your program. Using ternary operator.

我已经尝试过这段代码并且工作正常。请尝试你的程序。使用三元运算符。

$(document).ready(function(){
    $('ul.bxslider').bxSlider({
        controls: ($("#images div").length > 1) ? true: false,
    });
});

#2


0  

You are only setting up bxSlider if there is 1 image. I have also change .val() to .size() to return the amount of elements selected.

如果有1张图像,则只设置bxSlider。我还将.val()更改为.size()以返回所选元素的数量。

<script>
var imgCnt = $('#images').size(); //get images count.
var useControls = true; // Show controls?
if(imgCnt == 1){
    useControls = false;
}
$('.bxslider').bxSlider({
    controls:useControls
});
</script>

#1


0  

I have tried this code and it's working fine. Please try to your program. Using ternary operator.

我已经尝试过这段代码并且工作正常。请尝试你的程序。使用三元运算符。

$(document).ready(function(){
    $('ul.bxslider').bxSlider({
        controls: ($("#images div").length > 1) ? true: false,
    });
});

#2


0  

You are only setting up bxSlider if there is 1 image. I have also change .val() to .size() to return the amount of elements selected.

如果有1张图像,则只设置bxSlider。我还将.val()更改为.size()以返回所选元素的数量。

<script>
var imgCnt = $('#images').size(); //get images count.
var useControls = true; // Show controls?
if(imgCnt == 1){
    useControls = false;
}
$('.bxslider').bxSlider({
    controls:useControls
});
</script>