Horizontal scrolling div using buttons

前端 未结 3 1406
长情又很酷
长情又很酷 2020-12-30 08:56

How can I set left and right buttons on a DIV to scroll the content horizontally? I don\'t need to display a scrollbar.

HMTL:

相关标签:
3条回答
  • 2020-12-30 09:38

    Do:

    <div id="browser">
      <a href="#" id="left-button"><img src="left-button.jpg" /></a>
      <div id="content">
          <img src="thumb_1.jpg" />
          <img src="thumb_2.jpg" />
          <img src="thumb_3.jpg" /> 
          <img src="thumb_4.jpg" />
          <img src="thumb_5.jpg" />
          <img src="thumb_6.jpg" />
      </div>
      <a href="#" id="right-button"><img src="right-button.jpg" /></a>
    </div>
    
    <script>
       $('#right-button').click(function() {
          event.preventDefault();
          $('#content').animate({
            marginLeft: "-=200px"
          }, "fast");
       });
    </script>
    

    Then the same for the left button, except - marginLeft: +="200px".

    0 讨论(0)
  • Here is the code that you want. It's proved that works on IE, Safari, Chrome, Firefox, etc.

    Here is the HTML part.

        <div id="slide-wrap" style="width:1000px; height:200px; overflow:hidden; padding:0 auto;">
            <div id="inner-wrap" style="float:left;">
                <!-- 'Put Inline contains here like below.' -->
                <div class='containBox' style="width:250px; height:100%; float:left; display:inline-block;"></div>
                <!--  ...  -->
                <div class='containBox' style="width:250px; height:100%; float:left; display:inline-block;"></div>
                <!-- 'Put Inline contains here like above.' -->
            </div>
            <div style="display: block; width:40px; height:60px; position: absolute; margin-left:0px;   margin-top:40px">
                <img id='scroll_L_Arrow' src='../assets/img/l-arrow.png' onclick="scrollThumb('Go_L')"/>
            </div>
            <div style="display: block; width:40px; height:60px; position: absolute; margin-left:960px; margin-top:40px">
                <img id='scroll_R_Arrow' src='../assets/img/r-arrow.png' onclick="scrollThumb('Go_R')"/>
            </div>
        </div>
    

    Here is jQuery part in JavaScript function.

           function scrollThumb(direction) {
            if (direction=='Go_L') {
                $('#slide-wrap').animate({
                    scrollLeft: "-=" + 250 + "px"
                }, function(){
                    // createCookie('scrollPos', $('#slide-wrap').scrollLeft());
                });
            }else
            if (direction=='Go_R') {
                $('#slide-wrap').animate({
                    scrollLeft: "+=" + 250 + "px"
                }, function(){
                    // createCookie('scrollPos', $('#slide-wrap').scrollLeft());
                });
            }
           }
    

    For hiding the arrows please looking here. Detect end of horizontal scrolling div with jQuery

    0 讨论(0)
  • 2020-12-30 09:45

    you can try this: - make a "mask div" and set the position relative, set the width, and overflow hidden. - make a content div inside the mask div and set position fix

    Now you can put two button: left and right, and you need to modifiy only the content div's left value

    Similar as Patches solution :)

    0 讨论(0)
提交回复
热议问题