2012-08-06 93 views
0

我有一个图像滑块设置和工作,我现在需要的是显示或隐藏基于滑块内元素位置的图像。在Firebug根据位置显示或隐藏图像(图像滑块)

看,我可以看到以下内容(请注意,有几个里的项目,我只是显示一个在这里):

<li class="roundabout-moveable-item" style="position: absolute; left: 128px; top: 104px; width: 185.9px; height: 188.1px; opacity: 1; z-index: 145; font-size: 8.3px;"> 

现在根据该“左”的位置,我想能够做到以下几点:

if left > 400px then add a class which will show image one, 
if left < 300px then add a class which will show image two, 
if left >= 300px and <= 400px then add a class which will show no image 

非常感谢所有帮助。我是很新,JQuery的,但到目前为止,我想到的是:

var left = $("li.roundabout-moveable-item").position.left; 
$("li.roundabout-moveable-item").addClass("no-image"); 
if(left < 300px) { 
    $('li.roundabout-moveable-item").addClass("image-one"); 
} 
elseif(left > 400px) { 
    $('li.roundabout-moveable-item").addClass("image-one"); 
} 
else { 
    $('li.roundabout-moveable-item").addClass("no-image"); 
} 

感谢

+0

http://jsfiddle.net/XZXPh/检查此链接,并把您的影像检查你的代码。 – 2012-08-06 10:37:32

+0

好吧,所以我更新了jsfiddle,似乎唯一添加的类是.no-image类。 这将应用于所有列表项目,因此清单项目的“左”位置显然未被拾取。 任何人都可以提供任何想法为什么? – Johnny 2012-08-06 11:16:29

+0

好的,我可以看到,如果我添加一个警报(左);到我的代码,它显示为未定义。 – Johnny 2012-08-06 11:32:26

回答

0

只是从您的代码和正确的语法删除“PX”。 我认为你需要某事像这样:

var left = $(".roundabout-moveable-item").position.left; 
if(left < 300) { 
$(".roundabout-moveable-item").addClass("image-one"); 
} 
else if(left > 400) { 
$(".roundabout-moveable-item").addClass("image-two"); 
} 
else { 
$(".roundabout-moveable-item").addClass("no-image"); 
} 
+0

嗯,我猜你不能在一个类实际添加到类,所以需要将其添加到列表中的项目本身? 将编辑我的原始 – Johnny 2012-08-06 10:40:30

+0

通过使用addClass方法您的原始类(迂回可移动项目)将不会被删除。在html元素中将会有2个类以空格分隔。它应该工作正常。 – 2012-08-06 10:46:15

+0

谢谢伊霍尔,我想只是为了澄清我将类添加到.roundabout-movingable-item而不是li.roundabout-movingable-item 第一个仍然有效吗? – Johnny 2012-08-06 10:51:38