我想知道如何使用javascript进行以下自动滚动? carousol将使用HTML框放置在谷歌网站上,所以我不能只下载一个jquery插件,这将是最简单的解决方案! Jquery包含在脚本中。任何帮助或建议将不胜感激!这是代码:
<style>
.carousel {
width: 1080px;
height: 220px;
position: relative;
overflow: hidden;
background-color:black;
border-radius: 20px;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
margin-bottom: 20px;
margin-top: 20px;
}
.items {
width: 1080px;
position: absolute;
}
.items > div {
font-size: 20px;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.items > div > img {
width: 340px;
height: 202px;
padding: 10px;
}
.nav {
position: absolute;
bottom: 5px;
right: 15px;
}
.button {
cursor: pointer;
font-weight: bold;
color: #fff;
}
</style>
<div class="carousel" style="display:none;">
<div class="items">
<div>
<img src="http://i58.tinypic.com/2wq5nkk.png" border="0" alt="Speaker at event">
</div>
<div>
<img src="http://i62.tinypic.com/vfii61.png" border="0" alt="Speaker at event">
</div>
<div>
<img src="http://i59.tinypic.com/5ttg0z.png" border="0" alt="Speaker at event">
</div>
<div>
<img src="http://i61.tinypic.com/okpq9g.png" border="0" alt="Speaker at event">
</div>
<div>
<img src="http://i62.tinypic.com/2h4ywbo.png" border="0" alt="Speaker at event">
</div>
<div>
<img src="http://i60.tinypic.com/21oyg4x.png" border="0" alt="Speaker at event">
</div>
</div>
<div class="nav">
<span class="button left-button">prev</span>
<span class="button right-button">next</span>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script>
<script>
var current_slide = 0; // zero-based
var slide_count = 2;
var slide_size = 1080;
var Direction = {
LEFT: -1,
RIGHT: 1
};
/**
* Moves to the next slide using the direction (dx) parameter.
*/
var nextSlide = function(dx) {
current_slide = (current_slide + slide_count + dx) % slide_count;
// Calculate the new value for css 'left' property and animate.
var left_offset = '-' + (current_slide * slide_size) + 'px';
$('.items').animate({'left': left_offset}, 1080);
};
$('.right-button').click(nextSlide.bind(null, Direction.RIGHT));
$('.left-button').click(nextSlide.bind(null, Direction.LEFT));
$('.carousel').show();
</script>
答案 0 :(得分:1)
添加间隔的最简单方法,一遍又一遍地调用nextSlide。
var nextSlide = function(dx) {
current_slide = (current_slide + slide_count + dx) % slide_count;
// Calculate the new value for css 'left' property and animate.
var left_offset = '-' + (current_slide * slide_size) + 'px';
$('.items').animate({'left': left_offset}, 1080);
};
$('.carousel').show();
setInterval(function(){
nextSlide(Direction.LEFT);
}, 5000); // Will call nextSlide every 5 seconds