2017-03-19 37 views
-2

有人可以找到此代碼中的錯誤?當我用這個破碎的jQuery代碼運行它時,除了正文樣式,我的整個網站都消失了。我的網站太大,無法發佈整個html,css,jq代碼... 對於我想要用下面的代碼進行更好的代碼的建議非常受歡迎。代碼首先激活拖動.Box1,.Box2 ...然後它動畫移動到不同的位置。我只是想要切換.smartBox點擊時去點A,當點擊時再點B點。jQuery中的錯誤如果...其他語句

在此先感謝。

/* .smartBox on click: actiave dragging */ 
 
    var smartBoxClicked = false; 
 
    
 
    
 
    if(smartBoxClicked = false) { 
 
     
 
     $('.smartBox').click(function() { 
 
      $('.Box1, .Box2, .Box3, .Box4').draggable(); 
 
      $(this).animate({'top': '1%'}, 1000).animate({'left': '48%'}, 1000); 
 
      smartBoxClicked = true; 
 
     } else { 
 
      $('.smartBox').click(function() { 
 
       $(this).animate({ 
 
        'top': '370px' 
 
       }, 1000).animate({'left': '32.26%'}, 1000); 
 
       smartBoxClicked = false; 
 
     }); 
 
    }); 
 
    }; 
 
    
 
    /* .smartBox is a div with an image */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+1

有很多語法錯誤的 – NineBerry

+1

你的大括號不匹配,在所有 – Psi

+0

'= '賦值,'=='驗證值並且可以與不同的數據類型一起工作,'==='嚴格值驗證不適用於不同的數據類型 – Baruch

回答

0

應該

var smartBoxClicked = false;  

if(smartBoxClicked == false) { 
    $('.smartBox').click(function() {   
     $('.Box1, .Box2, .Box3, .Box4').draggable(); 
     $(this).animate({'top': '1%'}, 1000).animate({'left': '48%'}, 1000); 
     smartBoxClicked = true; 
    }); 
}else { 
    $('.smartBox').click(function() { 
     $(this).animate({ 
      'top': '370px' 
     }, 1000).animate({'left': '32.26%'}, 1000); 
     smartBoxClicked = false; 
    }); 
}; 

「=」 是賦值, 「==」 應該是比較,也是你的大括號不匹配。

+0

不能相信我沒有看到......:D謝謝 – Cass

0

在使用肘類此情況下,是很容易的:

.animated{ 
    top: 1%; 
    left: 48%; 
} 
.smartBox 
{ 
    transition-duration: 1s; 
} 

和jQuery:

$('.smartBox').click(function() { 
     $(this).toggleClass('animated'); 
} 
+0

謝謝你的想法,我甚至可能會切換到toggleClass =] – Cass

+0

歡迎您。如果你想繼續你的方式,你不需要'if(smartBoxClicked == false)'。它更快,更乾淨:'if(!smartBoxClicked)'。 –