I have written a script to make a div scroll when the user hovers over a button, but I would like to modify this code to allow multiple instances of this to take effect on one page.
I'm new to JQuery, so i can't figure out the way to do it
To clarify I would like multiple copies of the "container" div, all to work the same way, with their own buttons, using ths same script.
HTML:
<div class="container">
<div class="content">
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
<div class="product">
<p>string</p>
</div>
</div>
<a href="#" id="left">left</a>
<a href="#" id="right">right</a>
JQuery:
$(document).ready(function () {
var products = $('.product').length;
var width = products * 100;
$('.content').css('width', width + 'px');
if ($('.content').width() > $('.container').width()) {
$("#left").hover(function () {
animateContent("left");
}, function () {
$('.content').stop();
});
$("#right").hover(function () {
animateContent("right");
}, function () {
$('.content').stop();
});
}
});
function animateContent(direction) {
var animationOffset = $('.container').width() - $('.content').width();
if (direction == 'left') {
animationOffset = 0;
}
$('.content').animate({
"marginLeft": animationOffset + "px"
}, "fast");
}