Jquery horizontal Scroll using buttons

前端 未结 3 1032
南方客
南方客 2020-12-05 16:41

I am trying to obtain horizontal scroll using buttons.

I have a container which has several divs stacked horizontally and I want to scroll through them using the but

相关标签:
3条回答
  • 2020-12-05 16:50
    $('#right-button').click(function() {
      event.preventDefault();
      $('#content').animate({
        scrollLeft: "+=200px"
      }, "slow");
    });
    
     $('#left-button').click(function() {
      event.preventDefault();
      $('#content').animate({
        scrollLeft: "-=200px"
      }, "slow");
    });
    

    Edit, to explain... you need to set its scroll left position.

    DEMO PLUNKR

    0 讨论(0)
  • 2020-12-05 17:02

    You are looking for scrollLeft not marginLeft:

    $('#right-button').click(function() {
      event.preventDefault();
      $('#content').animate({
        scrollLeft: "+=200px"
      }, "slow");
    });
    
     $('#left-button').click(function() {
      event.preventDefault();
      $('#content').animate({
        scrollLeft: "-=200px"
      }, "slow");
    });
    

    Demo: http://plnkr.co/edit/ZdCw7IEYdV5YVeGg33oX?p=preview

    0 讨论(0)
  • 2020-12-05 17:11

    The answer of @Vennik is truely awesome,
    But in my case i used it bit differently, As i used Material Design and was making API call to display Image Carousal ,
    I did in this way ,
    First part is Carousel or Slider code and Second part is JS code

    <!-- Carousel -->
            <span style="cursor:pointer" id="left-button"><i class="material-icons">keyboard_arrow_left</i></span>&nbsp
    
            <span style="cursor:pointer" id="right-button"> <i class="material-icons">keyboard_arrow_right</i></span>
    
        <div class="bill-screens mdl-shadow--4dp" id="offer-pg-cont">
    
               <?php for($t=0; $t< count($arr_get_a_user['categories']);$t++){?>
                <div class="bill-pic bill-screen">
                  <button class="bill_class" id="bill_<?php echo $t ?>"  onClick="display_image()">
    
                    <img class="bill-screen-image" src="<?php echo $btn_img1; ?>">
                 </button>
    
                </div>
               <?php }?>
    
            </div>
    
    . . . . . . . . .  .
    
    <script data-require="jquery" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script type="text/javascript">
            // Carausol JS 
    
       $('#right-button').click(function() {
          event.preventDefault();
          $('#offer-pg-cont').animate({
            scrollLeft: "+=200px"
          }, "slow");
       });
    
         $('#left-button').click(function() {
          event.preventDefault();
          $('#offer-pg-cont').animate({
            scrollLeft: "-=200px"
          }, "slow");
       });
        </script>
    
    0 讨论(0)
提交回复
热议问题