Problem targetting only specified element
I've been working on a small jQuery plugin that creates an iPhoto-like preview when hovering over images. It all works great if I only have one set of images. As soon as I add more than one .preview set to the markup my plugin finds all of them and displays the total amount of indicators below each image.
Here is the JS:
$(document开发者_如何学Go).ready(function() {
// launch preview browser
$('.preview').previewBrowser();
});
(function($) {
$.fn.previewBrowser = function() {
return this.each(function() {
// get dom
var $viewport = $(this).css('overflow', 'hidden'),
$container = $viewport.children('ul'),
$items = $container.children('li'),
$single = $items.filter(':first');
// set viewport to correct size
$viewport.css({ height: $single.height(), width: $single.width() });
// set container to correct width
$container.css({ height: $single.height(), width: $items.length * $single.width() });
// float items
$items.css({ float: 'left' });
// add indicator to dom
var indicator = '';
for (i = 0; i < $items.length; i++) {
indicator += '<li class="left">O</li>';
}
$(indicator).appendTo('.indicator');
// set default indicator
$('.indicator li:eq(0)').css('color', 'red');
// set scrolling position while mouseover
$viewport.bind('mousemove.previewBrowser', function(evt) {
x = evt.pageX - this.offsetLeft;
offset = Math.floor(x / ($single.width() / $items.length)) * $single.width();
$(this).animate({ scrollLeft: offset }, 1);
// get current item
currentItem = (offset / $single.width());
// set current color
$('.indicator li').not('.indicators li:eq(' + currentItem + ')').css('color', 'black');
$('.indicator li:eq(' + currentItem + ')').css('color', 'red');
return false;
});
// set default image on mouseout
$viewport.bind('mouseleave.previewBrowser', function(evt) {
$(this).animate({ scrollLeft: 0 }, 1);
// set current color
$('.indicator li').not('.indicator li:eq(0)').css('color', 'black');
$('.indicator li:eq(0)').css('color', 'red');
});
});
};
})(jQuery);
And here is the markup:
<div id="content">
<div class="entry">
<div class="preview">
<ul class="container">
<li><img height="350" src="images/digitalsamba_1.png" width="800" /></li>
<li><img height="350" src="images/digitalsamba_2.png" width="800" /></li>
</ul>
</div><!-- end preview -->
<div class="description">
<div class="caption">
<h2>CloudApp</h2>
<p>
<strong>Product:</strong> CloudApp / <strong>Type:</strong> Development, Icon, Interface
</p>
</div><!-- end caption -->
<div>
<ul class="indicator"></ul>
</div><!-- end indicator -->
</div><!-- end description -->
</div><!-- end entry -->
<div class="entry no_border">
<div class="preview">
<ul class="container">
<li><img height="350" src="images/digitalsamba_1.png" width="800" /></li>
<li><img height="350" src="images/digitalsamba_2.png" width="800" /></li>
</ul>
</div><!-- end preview -->
<div class="description">
<div class="caption">
<h2>Canon Lense</h2>
<p>
<strong>Product:</strong> Canon / <strong>Type:</strong> Icon
</p>
</div><!-- end caption -->
<div class="indicator"></div><!-- end indicator -->
</div><!-- end description -->
</div><!-- end entry -->
</div><!-- end content -->
I know I am targeting the items wrong but I just can't figure out how to do it correctly.
I think you should take a look at this tutorial describing OO jQuery plugins. The problem with your plugin is that it is just running on one 'set', not creating a new object for each set.
try
$(document).ready(function() {
// launch preview browser
$(".entry .preview").each(function(){
$(this).previewBrowser();
});
});
This will iterate through all div.preview
in div.container
and apply the previewBrowser
individually
精彩评论