javascript用于带有html5的自动图像滑块
javascript for auto image slider with html5
下面的代码是我的图像滑块html页面,但在滑动最后一个图像后不会继续。
<style type="text/css">
#slideshow {
position:relative;
height:300px;
}
#slideshow IMG {
position:absolute;
top:0;
left:0;
z-index:8;
}
#slideshow IMG.active {
z-index:10;
}
#slideshow IMG.last-active {
z-index:9;
}
</style>'
<script>
function slideSwitch() {
var $active = $('div#slideshow IMG.active');
var $next = $active.next();
$next.addClass('active');
}
$(function() {
setInterval( "slideSwitch()", 5000 );
});
</script>'
<body>
<div id="slideshow" style="height:300px; width:100%">
<img src="${context:layout/images/images.jpeg}" title="Funky roots" style="position:absolute; height:300px; width:100%;" class="active"/>
<img src="${context:layout/images/police.jpg}" title="The long and winding road" style="position:absolute; height:300px; width:100%;"/>
<img src="${context:layout/images/viper_1.jpg}" title="Happy trees" style="position:absolute; height:300px; width:100%;"/>
</div>
</body>
从这个代码中,我如何获得自动滑动,并在最后一张图片后继续到下一张幻灯片?
您不会将last-active
类添加到最后一个元素中。所以每个元素都有active
类,每个元素都具有z-index: 10
在代码上做了一些工作之后,为您获得了这个新的JS:
function slideSwitch() {
var $active = $('div#slideshow IMG.active');
var $next = $active.next();
$active.addClass('last-active');
$active.removeClass('active');
if($next.length == 0) {
$next = $('div#slideshow IMG').first(); //at the end we need to select the first element, because there is no next()
}
$next.removeClass('last-active');
$next.addClass('active');
}
编辑:
回答您的意见:
在第一轮通过滑块后,类看起来像这样:
<img ... class="last-active"/>
<img ... class="last-active"/>
<img ... class="active"/>
当我们输入代码$active
时,它包含最后一个div
,而$next
为空,因为没有下一个元素。
在这一点上,我们必须告诉代码,下一个元素将是第一个。$next = $('div#slideshow IMG').first();
只是选择我们想要重新开始的第一个div。
相关文章:
- 用于图像和基于浏览器的图表的图表框架
- CodeIgniter:用于获取包括图像在内的值的控制器
- 用于将html转换为bbcode的Javascript,用于带有链接的图像
- Javascript没有'不适用于图像滑块(帮助)
- 将字节转换为用于在HTML5画布上绘制的图像
- 图像加载不适用于 IE 8 或更低版本
- 用于交换图像的 Javascript
- 用于图像的 Javascript 函数
- 背景图像不适用于 encodeURIComponent
- 用于插入图像的 JavaScript
- 用于图像上传的 ng 文件上传不起作用
- javascript用于带有html5的自动图像滑块
- 用于交换图像集的按钮
- 用于基于 PHP 变量更改图像的 JavaScript
- css:拖动图像不'不适用于Firefox
- 图像宽度上的转换不适用于窗口滚动
- 用于图像上映射区域的Bootstrap Popover/Tooltip
- Three.js-无法将本地图像用于loadTexture
- Node.js -将画布保存为图像[用于使用Amazon S3的Heroku托管应用程序]
- 延迟图像用于显示目的,而不是使用javascript的效率