Reputation: 1509
I was wondering how I can make the following autonmously scroll using javascript? the carousol is going to be placed on google sites using the HTML box so I can't just download a jquery plugin which would be the easiest solution! Jquery is included in the script. Any help or advice would be greatly appreciated! here is the code:
<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>
Upvotes: 2
Views: 197
Reputation: 406
The easyest way it to add an interval, calling the nextSlide over and over.
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
Upvotes: 1