English 中文(简体)
jQuery image carousel - How can I repeat the images?
原标题:

I ve written an image slideshow in jQuery that speeds up/down based on where the mouse is hovering over the images.

I d like to have the images repeat as the slideshow ends. So the user scrolls through the slideshow, it reaches the end of the image LI s and then seamlessly repeats from the start.

Here s the current code:

jQuery

$(document).ready(function()
{
   $( #products ul ).css( width , ($( #products ul li ).length * 185));

   var $products = $( #products div );
   var posLeft   = 0;

   // Add 5 DIV overlays to use as  hover buttons  for the slideshow speed
   for (i = 0; i <= 4; i++)
   {
      $( #products ).append( <div class="hover-  + i +  " style="background: transparent; width: 100px; height: 167px; position: absolute; left:   + posLeft +  px; top: 15px;"></div> );
      posLeft += 100;
   }  

   // Function animate the scrolling DIV in the appropriate direction with the set speed
   function plz2scroll(direction, scrollSpeed)
   {
      var scrollDir    = (direction ==  left )  ?  -=   :  += ;
      var scrollOffset = (direction ==  right ) ?  -10  :  +  + $products.css( width ).substring(0, -2) + 10; // Fix the  +100px10  issue - Seems substring don t support negative offsets 

      $products.animate({scrollLeft: scrollDir + $products.css( width )}, scrollSpeed,  linear , function()
      {
         $products.scrollLeft(scrollOffset);
         plz2scroll(direction, scrollSpeed);
      });
   }

   // Create the  hover buttons 
   $( div.hover-0 ).hover(function() { $products.stop(); plz2scroll( right , 2000); });
   $( div.hover-1 ).hover(function() { $products.stop(); plz2scroll( right , 3500); });
   $( div.hover-2 ).hover(function() { $products.stop(); });
   $( div.hover-3 ).hover(function() { $products.stop(); plz2scroll( left ,  3500); });
   $( div.hover-4 ).hover(function() { $products.stop(); plz2scroll( left ,  2000); });
});

HTML

<div id="products">
   <div>
      <ul>
         <li><img src="images/1.jpg" /></li>
         <li><img src="images/2.jpg" /></li>
         <li><img src="images/3.jpg" /></li>
         <li><img src="images/4.jpg" /></li>
         <li><img src="images/5.jpg" /></li>
      </ul>
   </div>
</div>

The scroller works and the speed/direction works nicely with the overlayed DIV buttons. My animate() callback to repeat is slow, buggy and just bad :/

My overuse of .stop() also looks like a potential problem >.<

Any ideas?

问题回答

just thought of something: You could try to position the inner div (the one obtained by $( #products div ) with position: relative; and the lis with position: absolute. Moreover, the inner div must have overflow: hidden (probably already the case). The positions of the list items are now relative to the surrounding div, which makes calculations easier.

Then, instead of moving the div as a whole, move the images individually. You can check if an item is visible by something like this, for instance:

function isItemVisible(li) {
    var w = li.outerWidth()
    var pos = li.position();
    return pos.left + w > 0 && pos.left < $products.innerWidth();
}

You still have to figure out when to rearrange individual items, but maybe this helps you get started. Maybe you will have to maintain a separate data structure where you can reorder the items when necessary, or at least maintain a leftmost and rightmost pointer to the (currently) leftmost and rightmost item.





相关问题
Using QCView and iSight to capture image

I have a QCView that loads a Quartz file which gives you iSights feedback (basically like a QTCaptureView) Everything displays fine The button simply takes a snapshot using the following simple ...

Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Transform rectangular image into trapezoid

In .NET how can I transform an image into a trapezoid. The Matrix class supports rotation, shear, etc, but I can t see a trapezoidal transformation. I m using the usual System.Drawing.* API, but I m ...

Rotate image through Y-axis on web page

What are my options for rotating an image on a web page through the Y-axis? I m not sure if I even have the terminology right. Is this called a rotate or a transform. Most of the searches that I ve ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

Convert from 32-BPP to 8-BPP Indexed (C#)

I need to take a full color JPG Image and remap it s colors to a Indexed palette. The palette will consist of specific colors populated from a database. I need to map each color of the image to it s "...

热门标签