2017-07-04 83 views
0

你能幫助理解爲什麼shake effect在我的情況下不起作用嗎? 代碼:搖動效果不起作用

$('.vote').on('click', function (e) { 
     e.preventDefault(); 
     $("body, html").animate({ 
      scrollTop: $($(this).attr('href')).offset().top 
     },{ 
      duration: 600, 
      complete: $('#shake').effect('shake') 
     }); 
    }); 

我也試圖把effectpreventDefault後卻又沒有成功。如果我將它稱爲on click事件以外,它將正常工作。 提前謝謝!

編輯: HTML部分:

<div class="col-xs-6 col-sm-6"> 
           <span class="upper"> 
            <a href="#force-reg"><span style="cursor:pointer;font-size: 40px;color: deepskyblue" class="unsigned glyphicon glyphicon-thumbs-up"></span></a> 
           </span> 
            <span style="font-size: 40px; color: grey" class="thumb-post-up_<?= $post->id ?>"><?= $vote_up ?></span> 
           </div> 
           <div class="col-xs-6 col-sm-6"> 
           <span class="upper"> 
            <a href="#force-reg"><span style="cursor:pointer;font-size: 40px;color: grey" class="unsigned glyphicon glyphicon-thumbs-down"></span></a> 
           </span> 
            <span style="font-size: 40px; color: grey" class="thumb-post-down_<?= $post->id ?>"><?= $vote_down ?></span> 
           </div> 

#shake格:

<?php if(!(\Yii::$app->user->isGuest)) : ?> 
      <div class="col-md-6 text-right custom-title"> 
       <span style="cursor:pointer;margin-right: 12px;" id="add-comment" class="button button-accent">Добави коментар</span> 
      </div> 
     <?php else : ?> 
      <div id="shake" class="col-md-6 col-sm-6 col-xs-12 text-right custom-title"> 
       <a href="http://find-doctor.webbuild.com/user/register"><span style="cursor:pointer;margin-right: 12px;"class="button button-accent">Само регистрираните потребители могат да коментират и гласуват!</span></a> 
      </div> 
     <?php endif; ?> 
+0

請分享你的HTML代碼,以及 – Shiladitya

+0

我創建了一個簡單的例子https://jsfiddle.net/qoaqw3hg/1/ – Shiladitya

+0

在您的示例中無法看到按鈕。而我的代碼仍然無法工作:) –

回答

1

對象的使用問題,在你的榜樣,你正在試圖獲得的HREF點擊鏈接,但這個是指當前功能動畫。我已經創建您的代碼示例,試試這個:

$('.vote').on('click', function(e) { 
 
    e.preventDefault(); 
 
    $("body, html").animate({ 
 
    scrollTop: $('.vote').offset().top 
 
    }, { 
 
    duration: 600, 
 
    complete: $('#shake').effect('shake') 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
      <div id="shake" class="col-md-6 col-sm-6 col-xs-12 text-right custom-title"> 
 
       <a href="http://find-doctor.webbuild.com/user/register"><span style="cursor:pointer;margin-right: 12px;"class="button button-accent">Само регистрираните потребители могат да коментират и гласуват!</span></a> 
 
      </div> 
 
      
 
      <a href="#" class="vote">vote </a>

+0

謝謝!它以這種方式工作! –