我有这个JavaScript代码:防止在jquery中单击多个点击!
var next = jQuery('#next') ;
var prev = jQuery('#prev') ;
var Scroller = jQuery('#Scroller') ;
var ScrollerTable = Scroller.children('table').width() ;
next.click(function(){
console.log('width of scroller table ' + ScrollerTable) ;
var offsetLeft = parseInt(Scroller.css("left")) ;
console.warn(offsetLeft) ;
if(offsetLeft == 0)
{
Scroller.animate({left : -140 } , 1000) ;
}else{
console.warn(offsetLeft) ;
if(ScrollerTable <= offsetLeft )
{
console.warn(offsetLeft) ;
alert('you are reached the end of scroller use the other btn') ;
return false ;
}else{
Scroller.animate({left : offsetLeft-140 } , 1000) ;
}
}
});
prev.click(function(){
var offsetLeft = parseInt(Scroller.css("left")) ;
console.warn('offsetLeft in backward ' + offsetLeft) ;
if(offsetLeft != 0 || offsetLeft > 0)
{
Scroller.animate({left : offsetLeft +140 } , 1000) ;
}else{
jQuery(this).css('cursor' , 'none') ;
return false ;
}
});
当上一个或下PERV多个动画用户点击不会完全完成,offsetLeft值不正确!我能做些什么来防止这种情况
我的第二个问题是当用户点击下一个btn滚动div滚动到-140px左边这个动作必须迭代直到达到幻灯片结束然后我必须禁用下一个btn。所以我得到一个变量的ScrollerTable宽度,并检查它与offsetLeft值,但它不工作!
,这里是HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div style="border:#ccc 1px solid ; padding:1px" class="scroll">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="2%"><img src="../images/dum/scroll_left.gif" width="20" height="165" id="prev" style="cursor:pointer" /></td>
<td width="96%" valign="top">
<div style="width:720px; height:165px ; position:relative ; overflow:hidden " id="Container">
<div style="position:absolute ; left:0" id="Scroller">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
{foreach from=$products item=p}
<td>
<div style="border:#ccc 2px solid ; padding:0px;margin:20px;">
<img src="imgsize.php?w=100&h=100&img=../uploads/product/{$p.xproductid}.jpg" />
<div style="background-color:#ccc ;text-align:center ; padding:5px; ">{$p.xproductname}</div>
</div>
</td>
{/foreach}
</tr>
</table>
</div>
</div>
</td>
<td width="2%"><img src="../images/dum/scroll_right.gif" width="20" height="165" id="next"/></td>
</tr>
</table>
将问题分成两部分会更好。 – kgiannakakis 2009-06-19 13:36:48
最好使用有效的HTML,避免嵌套表格,并且不要滥用表格进行布局。 – Quentin 2009-06-19 13:47:38