Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal scrolling div using buttons

Tags:

jquery

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:

<div id="browser">
  <a href="#" id="left-button"><img src="left-button.jpg" /></a>
  <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" />
  <a href="#" id="right-button"><img src="right-button.jpg" /></a>
</div>

CSS:

#browser { float: left; width: 200px; overflow: hidden; white-space: nowrap; }
like image 253
MAX POWER Avatar asked Dec 19 '11 12:12

MAX POWER


2 Answers

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

like image 128
NOTSermsak Avatar answered Sep 17 '22 20:09

NOTSermsak


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".

like image 28
Patches Avatar answered Sep 21 '22 20:09

Patches