简体   繁体   English

jQuery:水平滚动到div

[英]jQuery: horizontal scroll to div

What I'm aiming to achieve in the end in something similar to the bbc site: http://www.bbc.co.uk with a side scroll from section to section. 我最终打算在类似于英国广播公司的网站上实现的目标: http ://www.bbc.co.uk,从一节到另一节的侧面滚动。 Here's my code and I'll explain the problem I'm facing: 这是我的代码,我将解释我面临的问题:
jsFiddle: http://jsfiddle.net/neal_fletcher/kzQFQ/2/ jsFiddle: http//jsfiddle.net/neal_fletcher/kzQFQ/2/
HTML: HTML:

<div class="wrapper">
    <div class="container">
        <div class="contentContainer red"></div>
        <div class="contentContainer blue"></div>
        <div class="contentContainer orange"></div>
    </div>
</div>

<div class="left">LEFT</div>
<div class="right">RIGHT</div>

jQuery: jQuery的:

$(document).ready(function () {
    $('.right').click(function () {
        $('.container').animate({
            'left': '-100%'
        });
    });
    $('.left').click(function () {
        $('.container').animate({
            'left': '0%'
        });
    });    
});

Firstly I don't know if it's possible to stack the .contentContainer divs next to each other without having to set a 300% width on the .container div. 首先,我不知道是否可以将.contentContainer div叠加在一起,而不必在.container div上设置300%的宽度。 As the site is going to be CMS I don't want to keep changing the width of the .container div to suit. 由于该网站将成为CMS,我不想继续改变.container div的宽度以适应。 There will only ever be one .contentContainer div in view at one time too, thus I've set the overflow to hidden. 一次只能看到一个.contentContainer div,因此我将溢出设置为隐藏。
I can't seem to figure out a nice scroll function too, the one I have currently only scrolls the .container div once by 100%, ideally I'd want this to work more like a slideshow, ie on a loop, if possible. 我似乎无法弄清楚一个漂亮的滚动功能,我目前只有一次滚动.container div一次100%,理想情况下我希望这更像幻灯片,即在循环中,如果可能的话。 Any suggestions would be greatly appreciated! 任何建议将不胜感激!

I have updated your JSFiddle . 我已经更新了你的JSFiddle With the code below you can count how many elements you got inside your slider and thereafter set the % width automatically. 使用下面的代码,您可以计算滑块内部的元素数量,然后自动设置%宽度。

var length = $('div .container').children('div .contentContainer').length;
$(".container").width(length*100 +'%');

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

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