doufei9805 2014-07-29 22:41
浏览 459
已采纳

根据div的内容左右滑动div?

First off, I do apologize if this is a bit vague. I will try my best to explain this as best as I can.

I am trying to create a slideshow affect with a div content.

the content of the div is not images but they are HTML stuff.... buttons to be exact.

I have a div with an overflow hidden and an inner div inside this div with a width of 500000px. I know this is a ridiculous width size but i only done this because the content of this div is pulled from a database and i don't know how much data might be in there in the future. i did try width:auto; but that didn't work.

anyway, this is what I have in my page:

<div style="padding-left:15px; overflow:hidden;">
<div class="innerBottom" style="width:500000px;">
<?PHP echo $date_list; ?>
</div>
</div> 

now, I need to create a function using jquery that would allow me to slide the innerBottom to left or right using two buttons.

for example if the content is pulled from the database and there is not enough content for the slide to work then the slide is disabled but if there is enough content then the slide to the right is enabled at first and when the slided to the right once, then the slide to left is enabled.

I hope this makes sense and someone could help me out with this.

I did try something like this using jquery:

    <script>


    $("#button-bottom").click(function () {
  $('.innerBottom').animate({'margin-left':'3px'});
}, function() {
 $('.innerBottom').animate({'margin-left':'1003px'});
});

</script>

but that only does the left slide once and the inner div disappears for good.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doushao8399 2014-07-29 23:48
    关注

    Here's one super simple example I wrote quickly.. see if you can transform it for your needs. Good luck!

    http://jsfiddle.net/e4g9h/1/

    HTML

    <div id="mainWrapper">
        <div id="wrapper">
            <div id="slider" style="">
                <div class="contentDiv">
                    <input type="button" class="buttons" value="Button 1"/>
                </div>
                <div class="contentDiv">
                    <input type="button" class="buttons" value="Button 2"/>
                </div>
            </div>
        </div>
        <div id="left" class="triggerSlide">Go Left</div>
        <div id="right" class="triggerSlide">Go Right</div>
    </div>
    

    CSS

    #mainWrapper{
       width: 800px;
       }
    
    #wrapper{
       width: 800px;
       height: 100px;
       overflow: hidden;
       border: 1px solid black;
       }
    
    #slider{
       width: 1600px;
       text-align: center;
       }
    
    .contentDiv{
       width: 800px; 
       float: left;
       }
    
    .buttons{
       width: 600px;
       height: 90px;
       }
    
    .triggerSlide{
       text-align: center;
       padding: 5px;
       display: inline-block;
       width: 100px;
       border: 1px solid red;
       cursor: pointer;
       }
    

    Jquery

    $(document).ready(function(){
    var margin = 0;
    $('.triggerSlide').on('click',function(){
        var amountDivs = $('.contentDiv').length;
        var whereTo = $(this).attr('id');
    
        whereTo == 'left' ? margin -= 800 : margin += 800;
    
        if(parseInt(margin) > 0){
            margin = 0;
            return false;
            }
        else if(margin <= -Math.abs(amountDivs * 800)){
            margin = -Math.abs((parseInt(amountDivs) -1) * 800);
            return false;
            }
    
        $('#slider').animate({
            marginLeft: margin
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献