简体   繁体   English

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

[英]slide div left and right depending on the content of the 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. 我正在尝试创建一个具有div内容的幻灯片显示效果。

the content of the div is not images but they are HTML stuff.... buttons to be exact. div的内容不是图像,而是HTML内容。...确切地说是按钮。

I have a div with an overflow hidden and an inner div inside this div with a width of 500000px. 我有一个overflow hiddenoverflow hidden的div,并且此div内部有一个内部div,宽度为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. 我知道这是一个荒谬的宽度大小,但我这样做是因为此div的内容是从数据库中提取的,而且我不知道将来可能有多少数据。 i did try width:auto; 我确实尝试过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. 现在,我需要使用jquery创建一个函数,该函数将允许我使用两个按钮将innerBottom左右滑动。

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: 我确实尝试过使用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. 但这只会使左侧滑动一次并且内部div永远消失。

Thanks in advance 提前致谢

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/ http://jsfiddle.net/e4g9h/1/

HTML 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 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 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
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM