horizontal scrolling div onclick with javascript or jquery

前端 未结 4 1267
广开言路
广开言路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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:

    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

提交回复
热议问题