简体   繁体   中英

Disable next button and prev button in Carousel

I am using jquery carousel in my application like

            <script type="text/javascript">
     var j = jQuery.noConflict();
     j(document).ready(function () {
         j('#pagination').jcarousel({
             wrap: 'circular'
         });
         var count = $("#pagination li").size();
         if (count < 10)
         {
             j(".jcarousel-prev").addClass("jcarousel-prev-disabled");
             j(".jcarousel-next").addClass("jcarousel-next-disabled");
         }
     });

     var prm = Sys.WebForms.PageRequestManager.getInstance();

     prm.add_endRequest(function() {
         var j = jQuery.noConflict();
         j(document).ready(function () {
             j('#pagination').jcarousel({
                 wrap: 'circular'
             });
             var count = $("#pagination li").size();
             if (count < 10) {
                 j(".jcarousel-prev").addClass("jcarousel-prev-disabled");
                 j(".jcarousel-next").addClass("jcarousel-next-disabled");
             }
         });
     });
   </script>

Now the issue is that it doesn't make next button and prev button disabled if there are li items less than 10... Please help!!!

Add css to your class like

.jcarousel-prev-disabled, 
.jcarousel-next-disabled {
    pointer-events: none;
    cursor: default;
    opacity: 0.6;
}

Updated

Use $("#pagination li").length instead of $("#pagination li").size() and your problem will solve.

I've downloaded the jQuery carousel plugin and created the example below for you.The example below will disable the prev & next buttons.If you want to completely hide them you can use $(".jcarousel-control-next").css('display', 'none');

   <head>
    <title>Basic carousel - jCarousel Examples</title>
    <link rel="stylesheet" type="text/css" href="../_shared/css/style.css">
    <link rel="stylesheet" type="text/css" href="jcarousel.basic.css">
    <script type="text/javascript" src="../../vendor/jquery/jquery.js"></script>
    <script type="text/javascript" src="../../dist/jquery.jcarousel.min.js"></script>
    <script type="text/javascript" src="jcarousel.basic.js"></script>
    <script type="text/javascript">
        $(function(){

            var imageCount = $("#images li").length;                    
            if(imageCount < 10){
                $(".jcarousel-control-next").css('pointer-events', 'none');
                $(".jcarousel-control-prev").css('pointer-events', 'none'); 
            }
        });
    </script>
</head>
<body>
    <div class="wrapper">
        <div class="jcarousel-wrapper">
            <div class="jcarousel">
                <ul id="images">
                    <li><img src="../_shared/img/img1.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img2.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img3.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img4.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img5.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img6.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img1.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img2.jpg" width="600" height="400" alt=""></li>
                    <li><img src="../_shared/img/img3.jpg" width="600" height="400" alt=""></li>
                </ul>
            </div>
            <a href="#" class="jcarousel-control-prev">&lsaquo;</a>
            <a href="#" class="jcarousel-control-next">&rsaquo;</a>
            <p class="jcarousel-pagination">
            </p>
        </div>
    </div>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM