2017-07-30 57 views
0

我已经使用了2个锚点标签(#trendingnexticon和#trendingpreviousicon)来移动 容器的内容,当我点击标签时内容完全向右或向左移动但它只能移动一次。当我再次点击时,它不会移动!有什么建议么?JQuery-animate()只能在点击时工作一次

的Javascript:

$(document).ready(function(){ 
     $("#trendingnexticon").on('click' ,function(){ 
      $("#trendingtable").animate({right: '800px'}); 
     }); 
    }); 


    $(document).ready(function(){ 
     $("#trendingpreviousicon").on('click' ,function(){ 
      $("#trendingtable").animate({left: '100px'}); 
     }); 
    }); 

HTML:

<div id="trendingdiv" class="" style="overflow-x:scroll; overflow: 
    hidden;"> 
     <table id="trendingtable" class="table" style="position:relative;"> 
     <a id="trendingpreviousicon" style="cursor:pointer; margin-top: 
    62px; position:absolute; z-index:1;" class="previous round">&#8249;</a> 
     <a id="trendingnexticon" style="cursor:pointer; margin-left: 1250px; 
    margin-top: 62px; position:absolute; z-index:1;" class="next 
    round">&#8250;</a> 
     <tr> 
     <?php while ($tsingers = mysqli_fetch_assoc($tsingersquery)): ?> 
      <td> 
       <div class="media" style="border: 1px solid #e6e6e6; 
    width:400px; padding: 0px;"> 
       <img src="images/<?=$tsingers['image'];?>" alt="<? 
    =$tsingers['name'];?>" 
       class="pull-left img-responsive" style="width: 200px; height: 
    150px;" id="artistimg"> 
       <div id="trendingmediabody" class="media-body"> 
       <p id="trendingname" style="font-size:14px; font-weight: 
    bolder;"><?=$tsingers['name'];?></p> 
       <p id="trendingcategory" style="font-size:12px; font-weight: 
    bolder;"><?=$tsingers['category'];?></p></br></br> 
       </div> 
      </div> 
      </td> 
     <?php endwhile; ?> 
     </tr> 
     </table> 
    </div> 

回答

1

通过执行$("#trendingtable").animate({right: '800px'});你实际上是说 “动画元素,直到它具有800像素从相对于它的父权的位置。”执行这两次不会产生任何效果。如果你想让它移动多次,你可以使用像

$("#trendingtable").animate({right: '+=800px'}); 

查看文档这里的相对运动:http://api.jquery.com/animate/

+0

谢谢!它现在可以工作了。但是我遇到了一个新问题。如果我点击左侧的图标一次,然后点击右侧的图标不起作用。右侧图标变为“禁用”一旦左侧图标被点击,是否有东西丢失? – 2chins