简体   繁体   English

javascript内容滑块仅显示第一张幻灯片

[英]javascript content slider displaying only first slide

I have a text slider on my home page , that should display slides individually according to which arrow is selected. 我的主页上有一个文本滑块,该滑块应根据选择的箭头分别显示幻灯片。 Currently, only the first slide is showing and each slide thereafter is blank. 当前,仅显示第一张幻灯片,此后每张幻灯片都是空白。 What am I missing to make each individual slide show? 制作每张幻灯片都缺少什么?

jsfiddle jsfiddle

jQuery(document).ready(function($){
    // Setup Variables
    var slides = $('#slider_mask .slide_container').children();
    var total_slides = slides.children().length;
    var slide_width = $('#slider_mask').width();
    var current_slide = 0;

    slides.not(':first').hide();
    // Set the width of the slide_container to total width of all slides
    $('#slider_mask .slide_container').width(slide_width*total_slides);

    // Handle Right Arrow Click
    $('#slider_mask .right_button').on('click', function() {

        current_slide++;

        if(current_slide == total_slides){ current_slide = 0; }

        var negative_margin_required = current_slide*slide_width;
        $('#slider_mask .slide_container').stop().animate({marginLeft:-negative_margin_required+'px'},'fast');


    });

    // Handle Left Arrow Click
    $('#slider_mask .left_button').on('click', function() {

        current_slide--;

        if(current_slide < 0){ current_slide = total_slides-1; }

        var negative_margin_required = current_slide*slide_width;
        $('#slider_mask .slide_container').stop().animate({marginLeft:-negative_margin_required+'px'},'fast');

    });
});

Check out this fiddle . 看看这个小提琴 I scaled it down to a minimum. 我将其缩小到最小。

I started with the slider mask having the fixed width and set the other widths off of that. 我从固定宽度的滑块蒙版开始,然后设置其他宽度。

Ludovico Grossi is correct about making the slide element float left. Ludovico Grossi关于使滑动元件向左浮动是正确的。 When they are "block" elements without the float, they stack. 当它们是没有浮点数的“块”元素时,它们会堆叠。 If they are changed to "inline" elements, they cannot have a fixed width. 如果将它们更改为“内联”元素,则它们不能具有固定的宽度。 It worked to set them to "inline-block", but I don't know if that is supported in all browsers. 它可以将它们设置为“内联阻止”,但是我不知道所有浏览器是否都支持。 Having them as "block" and float left works. 将它们作为“块”并向左浮动会起作用。

I also had to make some other changes. 我还必须进行一些其他更改。 One of which was that the .animate() function does not cause a hidden element to be shown. 其中之一是.animate()函数不会导致显示隐藏元素。 It says this on the documenation page . 它在文档页面上说了这一点。 Instead of hiding the slides by calling .hide() , they simply are out of view by having overflow set to hidden for the mask element. 与其通过调用.hide()隐藏幻灯片, .hide()通过将mask元素的溢出设置为hidden来隐藏幻灯片。

I also put the text-align center on the slide elements, rather than the container element. 我还将文本对齐的中心放在滑动元素上,而不是容器元素上。

UPDATE: 更新:

I created another fiddle that starts with the code from adeneo's fiddle and includes the necessary changes. 我创建了另一个小提琴 ,该小提琴adeneo的小提琴中的代码开始,并包含必要的更改。 I put comments next to all the code and CSS that was added, removed, or changed. 我在所有已添加,删除或更改的代码和CSS旁边放置注释。 I had to make one change to the html. 我必须对html进行一次更改。 I added the <div id="home"> element that wraps everything. 我添加了<div id="home">元素,该元素包装了所有内容。 Without it the CSS selectors don't match anything. 没有它,CSS选择器将不匹配任何内容。

Check your css: 检查您的CSS:

slide: fixed width, float left. 滑动:固定宽度,向左浮动。

container: width n_slide * slide_(outer)_width 容器:宽度n_slide * slide_(外部)_width

slide mask: the same as a single slide width, overflow hidden. 幻灯片蒙版:与单个幻灯片宽度相同,隐藏溢出。

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

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