2013-04-25 21 views
1

基本上我正在做的是製作一個「查看更多」類型的東西。我有一段文字描述了我所做的工作,並在段落末尾有一個按鈕。我(這是一個真正的鏈接,帶有按鈕樣式)。當按鈕被按下時div會擴大到400px,而額外的文字會被添加.html();但是當我點擊「查看少」按鈕折回到較小的段落和200px div時,沒有任何反應。jquery使用按鈕動畫高度變化

jQuery的

$(document).ready(function(){ 
var $mottheight = $('#row-mott').height(); 

if ($mottheight == 300) { 
    $('#mott-btn-collapse').click(function(){ 
    $('#row-mott').animate({ 
     height:200 
    }, 300); 
    $('#mott').html('I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. <a href="#" id="mott-btn" class="btn btn-mini">Less info &raquo</a>'); 

}); 
} 
else if ($mottheight < 300){ 
    $('#mott-btn').click(function(){ 
    $('#row-mott').animate({ 
     height:300 
    }, 400); 
    $('#mott').html('I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. <a href="#" id="mott-btn-collapse" class="btn btn-mini">Less info &raquo</a>'); 
    }); 
} 
else { 
    return 0; 
} 
}); 
+0

請記住,代碼是在加載時執行的。當頁面加載第一個if語句爲false時,所以點擊綁定永遠不會被加載。 – 2013-04-25 17:33:44

回答

4

你的功能是非常笨拙編碼。您需要在功能中捕捉高度,而不是僅在頁面加載時,並且可以合併所有點擊項目。

var $row_mott = $('#row-mott'), 
    $mott = $('#mott'); 

$('#mott-btn-collapse').click(function(){ 
    var $mottheight = $row_mott.height(); 

    if ($mottheight == 300) { 
     $row_mott.stop().animate({height:200}, 300); 
     $mott.html('I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. <a href="#" id="mott-btn" class="btn btn-mini">Less info &raquo</a>'); 
    } else if ($mottheight < 300){ 
     $row_mott.stop().animate({height:300}, 400); 
     $mott.html('I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. I\'ve worked on the Warren Mott High School website since September of 2011. I am one of four student webmasters that keep the website in the best condition possible. We cannot take credit for all the coding, though. This website is run by around 40 Web Design students. As Webmaster, my job is to fix, improve, and help those students learn as they go. <a href="#" id="mott-btn-collapse" class="btn btn-mini">Less info &raquo</a>'); 
    } 
}); 

注:

  • 我轉換你的選擇爲緩存變量;多次使用選擇器時的良好做法
  • 我在動畫之前添加了.stop()以防止過多排隊
  • 我將操作合併到一個按鈕中;這是更個人的偏好,但爲了更加一致的用戶體驗,只有一個「擴展/摺疊」按鈕是非常標準的
+0

再次打我!不錯的工作! – 2013-04-25 17:34:13

+0

這就是我在我的回答中試圖做的,除非我比原來更加尷尬,所以我剛刪除它。工作很好。 +1好友:) – War10ck 2013-04-25 17:40:20

+0

我目前遠離我的電腦,但當我回來時,我會試試這個。感謝所有的幫助! – RonnieEXP 2013-04-25 17:59:26

0

您只運行一次點擊處理程序。因此,在文檔加載時,它會找到#行 - mott高度,設置它,並評估您擁有的。默認情況下,它是< 300.您需要檢查點擊事件中的高度,而不是之前。

0

試試這個..

$(document).ready(function(){ 
    var $mottheight = $('#row-mott').height(); 
    $('#mott-btn-collapse').click(function(){ 
     if ($mottheight == 300) { 
    $('#row-mott').animate({ 
      height:200 
     }, 300); 
     $('#mott').html('I\'ve worked on the W...</a>'); 

    }); 
    }else if($mottheight < 300){ 
     $('#mott-btn').click(function(){ 
     $('#row-mott').animate({ 
      height:300 
     }, 400); 
     $('#mott').html('I\'ve worked on the Warren Mott....they go. <a href="#" id="mott-btn-collapse" class="btn btn-mini">Less info &raquo</a>'); 
     }); 
    } 
    }); 
0

我想通了。這是非常簡單的,我真的不知道爲什麼我以前沒有想到這一點。

$(document).ready(function(){ 

    $('#mott-appended').hide(); 
    $('#patrick-appended').hide(); 

    /* Start Warren Mott High School text show/hide */ 

    $('#mott-btn').click(function(){ 
    $('#mott-appended').show('slow'); 
    $(this).hide('slow'); 
    }); 

    $('#mott-btn-collapse').click(function(){ 
    $('#mott-appended').hide('slow'); 
    $('#mott-btn').show('slow'); 
    }); 

    /* End Warren Mott High School text show/hide */ 
    /* Start Patrick Studios text show/hide */ 

    $('#patrick-btn').click(function(){ 
     $('#patrick-appended').show('slow'); 
     $(this).hide('slow'); 
    }); 

    $('#patrick-btn-collapse').click(function(){ 
     $('#patrick-appended').hide('slow'); 
     $('#patrick-btn').show('slow'); 
    }); 

    /* End Patrick Studios text show/hide */ 

});