简体   繁体   English

CSS3图片滑块,通过幻灯片的javascript导航不起作用?

[英]CSS3 image slider, javascript navigation through slides not working?

I've been trying to adapt the following code to integrate with my CSS3 slider (animated and timed with keyframes) however as you can't use .animate in js when using css3 animations on the same element I either have to use one or the other. 我一直在尝试将以下代码与我的CSS3滑块集成(通过关键帧进行动画处理和定时),但是由于在同一元素上使用css3动画时无法在js中使用.animate,因此我必须使用一个或其他。

JS I've adapted for my slider JS我已经适应了我的滑块

The current js works in the sense that it navigates through the slides my only issue is that it doesn't 'slide' to each slide it jumps. 当前的js在某种意义上可以在幻灯片中导航,我唯一的问题是它不会“跳转”到它跳过的每张幻灯片。

I'd really like to keep the slideshow as it is and just want to update my js so that the slide transition works. 我真的很想保留幻灯片的原样,只想更新我的js,以便幻灯片过渡有效。 I'm not great with js so I've been finding it difficult to find a solution. 我对js不太满意,因此一直很难找到解决方案。 If anyone could give some advice or a solution to my problem it would be truly appreciated. 如果有人可以提出建议或解决我的问题,将不胜感激。

DEMO 演示

JS JS

//grab the width and calculate left value
var item_width = $("#carousel .video-list li").outerWidth();
var left_value = item_width * (-1);

//if user clicked on prev button
$('#previous').click(function () {
//get the right position            
var left_indent = parseInt($("#carousel .video-list").css('left')) + item_width;

    //slide the item
    $("#carousel .video-list").animate({'left' : left_indent}, function () {

    //move the last item and put it as first item                
    $("#carousel .video-list li:first").before($("#carousel .video-list li:last"));

    //set the default item to correct position
    $("#carousel .video-list").css({'left' : left_value});

    });    
//cancel the link behavior            
return false;
});

//if user clicked on next button
$('#next').click(function () {

    //get the right position
    var left_indent = parseInt($("#carousel .video-list").css('left')) - item_width;

    //slide the item
    $("#carousel .video-list").animate({'left' : left_indent}, function () {

    //move the first item and put it as last item
    $("#carousel .video-list li:last").after($("#carousel .video-list li:first"));

    //set the default item to correct position
    $("#carousel .video-list").css({'left' : left_value});
    });    
//cancel the link behavior
return false;

});

I don't see why .animate is needed, because I have used 我不明白为什么需要.animate,因为我已经使用过

transition: left 1s ease;

in my CSS to achieve the same things, with a smoother animation than I got with jQuery. 在我的CSS中实现比jQuery平滑的动画效果。 I tried deleting the: 我尝试删除:

//slide the item
$("#carousel .video-list").animate(...

for the left and right. 为左右。 I also added some text to the html divs so that you can see how it's moving better. 我还向html div中添加了一些文本,以便您可以看到它的运行情况如何更好。 Sorry that I couldn't get it working, but I really feel that "transition" is what you need to look into. 抱歉,我无法正常运行,但我真的觉得您需要研究“过渡”。 Here's the fiddle :) 这是小提琴 :)

... ...

I think your simplest solution would be to ditch the whole CSS animate , and build your own carousel: Consider a film strip, which is a bunch of pictures lined up next to each other. 我认为您最简单的解决方案是抛弃整个CSS 动画 ,并构建自己的轮播:考虑一个电影胶片,它是一排彼此并排排列的图片。 Now consider a paper with a box cut-out, the size of one picture. 现在考虑一张带有框形切口的纸张,即一张照片的大小。 We put the strip behind the paper, and see only one image. 我们将条带放在纸后面,只能看到一张图像。 If we move the strip, we can change which image we see. 如果移动条带,则可以更改看到的图像。

The film strip will be a div with { display: inline-block; 胶片条将与{显示一个div:内联块; } property, containing each of the videos. }属性,其中包含每个视频。 The box cut-out will be a div with { overflow: hidden } property. 框形切口将是具有{overflow:hidden}属性的div To move the strip, we simply use $('#strip').css({'left': positionofstripleft - widthofbox }) in our javascript. 要移动地带,我们只需在JavaScript中使用$('#strip')。css({'left':positionofstripleft-widthofbox}) And for the animation, a simple setInterval(Nextfunction, 65000) to do what clicking next would do every 65 seconds. 对于动画,一个简单的setInterval(Nextfunction,65000)可以执行每65秒单击一次next的操作。

Finally, a CSS transition will make the movements actually animated. 最后,CSS 过渡将使动作真正具有动画效果。

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

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