You'll notice the containment options for the draggable are [7, 0, 40, 0], though I feel they should be [0, 0, 32, 0]. If you click on the button so it is in the "ON" position and drag the <div>
(circle slider thing) around, you will notice that it goes to far left and not far enough right. I can't figure out why this is the case. Here is a picture of the problem, it is very slight.
This is what I am going for when you drag it around:
Thanks!
HTML:
<section></section>
<div></div>
CSS:
section {
background-image: url(http://www.rsadler.com/OnOff_slider_small.png);
width: 57px;
height: 25px;
margin: 0;
padding: 0;
}
section:hover{
cursor: pointer;
}
section.ON {
background-position: 82px 0px;
}
div {
background-image: url(http://www.rsadler.com/OnOff_slider_small.png);
background-position: 26px 0px;
position: relative;
height: 24px;
width: 25px;
margin: -25px 0px 0px 0px;
padding: 0;
}
div:hover {
cursor: pointer;
}
JS:
$(document).ready(function(){
$('section').click(function(){
if ($(this).hasClass('ON')) {
$(this).removeClass('ON');
$(this).next().slideLeft();
} else {
$(this).addClass('ON');
$(this).next().slideRight();
}
});
$('div').click(function(){
if ($(this).prev().hasClass('ON')) {
$(this).slideLeft();
$(this).prev().removeClass('ON');
} else {
$(this).slideRight();
$(this).prev().addClass('ON');
}
});
$('div').draggable({
axis: 'x',
containment: [7, 0, 40, 0] // why do I have to put 7 as the x1 value? It should be 0
});
});
jQuery.fn.slideRight = function() {
var o = $(this[0])
o.animate({
'left': '32px'
}, 300);
};
jQuery.fn.slideLeft = function() {
var o = $(this[0])
o.animate({
'left': '0'
}, 300);
};