4

I am trying to implement a carousel with JQuery based on http://www.queness.com/post/923/create-a-simple-infinite-carousel-with-jquery. It allmost works OK. Only when i swipe left and right i dont get back to my initial position. By debugging i found out that the left set is not set in my ul element. The code:

Javascript

var container = $('#calendarContainer ul');
var moveWidth = container[0].clientWidth / 3;
var currentIndent = container.position().left;


if (event.type == "swipeleft") {
   $('#calendarContainer ul').css('left', currentIndent - moveWidth);
}
if (event.type == "swiperight") {
   $('#calendarContainer ul').css('left', currentIndent + moveWidth);
}

Html

<div id="calendarContainer" >
    <ul>
        <li id ="calendarDivLeft">
            <fieldset id="calendarWeekGridLeft" class="ui-grid-a"></fieldset>
        </li>
        <li id ="calendarDiv" >
            <fieldset id="calendarWeekGrid" class="ui-grid-a"></fieldset>
        </li>
        <li id="calendarDivRight">              
            <fieldset id="calendarWeekGridRight" class="ui-grid-a"></fieldset>
        </li>
    </ul>
</div>

CSS

#calendarContainer ul 
{
     width : 300%;
     list-style: none;
     padding:0px;
     left : 0%;
     position:relative;
     margin: 0px;
     padding: 0px;
 }

 #calendarContainer ul li
 { 
     width: 33.333%; 
     float:left;   
     position:relative;
     margin: 0px;
     padding: 0px;
 }

For example if the current left is 5 and I move left by 594px, I would expect to have a left of -589. Instead I get a get a left of 585! If I move right by 595 the left becomes 605 instead of 600.

How is this possible?

6
  • 2
    a link to localhost will not help us. Commented Dec 29, 2011 at 10:43
  • Is there any padding, margin, or border on the element you're trying to update? Commented Dec 29, 2011 at 10:44
  • 1
    try $('#calendarContainer ul').css('left', (currentIndent - moveWidth) + 'px'); Commented Dec 29, 2011 at 10:44
  • can run your localhost url here. share some html over here.. or create a jsfiddle to show your problem. Commented Dec 29, 2011 at 10:45
  • I created a fiddle here jsfiddle.net/cHgHg/7 unfortunately(?) the problem does not occur here Commented Dec 29, 2011 at 11:02

2 Answers 2

4

Try this:

$('#calendarContainer ul').css('left', function(index, oldValue){
    var newValue = oldValue - moveWidth;
    return newValue;
});

The css() function can take a function whose argument are the index of the element in the set and the current value of the property that was referenced. It's just a matter of doing the math in this function and returning the new value.

Sign up to request clarification or add additional context in comments.

2 Comments

how would this yield a different result than what was posted?
i tried this (replace my css line with this) and now by li does not move at all. I probably don't understand this...
0

CSS likes to have units for measurements. Try specifying px

$('#calendarContainer ul').css('left', (currentIndent - moveWidth) + 'px');

1 Comment

adding px does not seem to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.