horizontal scrolling div onclick with javascript or jquery

前端 未结 4 1266
广开言路
广开言路 2021-02-04 17:11

I have spent the last two days looking for a simple javascript or jquery code for this. I want to incorporate a horizontal scrolling div using javascript or jquery to display i

相关标签:
4条回答
  • 2021-02-04 17:13

    You can just use code like this:

    function FixMargin(left) {
        $(this).css("left", left);
    }
    
    $(document).ready(function () {
    
    $('#rightbutton').click(function () {
        var left = parseInt($("#bitToSlide").css('left'), 10);
        $("#bitToSlide").animate({ left: left - pageWidth }, 400, FixMargin(left - pageWidth));
    });
    
    $('#leftbutton').click(function () {
        var left = parseInt($("#bitToSlide").css('left'), 10);
        $("#bitToSlide").animate({ left: left + pageWidth }, 400, FixMargin(left + pageWidth));
    });
    
    }
    

    where your html looks like this:

    <div id="slideleft" class="slide">
        <div class="inner" id="bitToSlide" style="width:1842px">
            <table border="0" cellpadding="0" cellspacing="0">
                <tr valign="top">
                    <td id="page1" style="width: 614px">
                    </td>
    
                    <td id="page2" style="width: 614px">
                    </td>
                </tr>
            </table>
        </div>
    </div>
    

    and your css looks like this:

    .slide
    {
    position:relative;
    overflow:hidden;
    height:365px;
    width:597px;
    margin:1em 0;
    background-color:#E9ECEF;
    border:0px
    }
    
    .slide .inner
    {
    position:absolute;
    left:0;
    bottom:0;
    height:365px;
    padding:0px;
    background-color:#E9ECEF;
    color:#333
    }
    

    I wrote the above a really long time ago - so you probably want to update it a bit (replace the table's with div's for example) but it works.

    You obviously need the two buttons on there as well

    0 讨论(0)
  • 2021-02-04 17:15

    You can have a look to iscroll 4.

    http://cubiq.org/iscroll-4

    You have a scrollTo method or scrollToElement method...

    0 讨论(0)
  • 2021-02-04 17:33

    You can do something like this. Not tested code but just giving you a hint.

    <div id="imageContainer">
      <div id="imageWrapper">
        <img />
        <img />
      </div>
      <span id="left">Left</span>
      <span id="right">right</span>
    </div>
    
    var imageWidth = 200;
    $("#left").click(function(){
      $("#imageWrapper").animate({marginLeft:"-="+imageWidth}, 500);
      //Offcourse you need to handle the corner cases when there is not image to move left
    });
    
    $("#right").click(function(){
      $("#imageWrapper").animate({marginLeft:"+="+imageWidth}, 500);
    });
    
    0 讨论(0)
  • 2021-02-04 17:36

    There are a lot of jQuery plugins which allow you to do this very easily. For ex: http://slidesjs.com/

    Hope this helps.

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