我想要改變div的margin-top。JQuery animate .css()而不是使用.anmiate()?
通常我會因此這爲這樣:
$("#mydiv").animate({ marginTop: '+='+myvar}, 200);
但這始終增加(或減少)的值。我想要一種簡單的方法來改變中的值。
例如:如果div開始時的margin爲100,myvar值爲50我想動畫div使其縮回到50.同樣,如果myvar值是200,我想動畫改變長到200
我能實現這個有點,但重置CSS,如:
$("#mydiv").css("margin-top", "0px");
$("#mydiv").animate({ marginTop: '+='+myvar}, 200);
每次但是這使得它有點笨重。
有沒有人知道可以做到這一點?
(PS:不使用CSS轉換)
編輯:添加我的代碼如下
$("#info1tab").click(function() {
$(".textbox").slideUp('200');
$("#info1").delay(300).slideDown('200');
var itemheight = $("#info1").css("height");
var itemheightint = parseInt(itemheight);
$("#photobox").animate({ marginTop: itemheightint }, 200);
});
$("#info2tab").click(function() {
$(".textbox").slideUp('200');
$("#info2").delay(300).slideDown('200');
var itemheight = $("#info2").css("height");
var itemheightint = parseInt(itemheight);
$("#photobox").animate({ marginTop: itemheightint }, 200);
});
通過本教程,並讓我知道這是否有幫助http://www.schillmania.com/content/projects/javascript-animation-1/ – 2013-03-01 07:35:10
你問這樣的事情? $('#left-bg).click( function(){(this).animate({'width':'100%'},600); }); – 2013-03-01 07:40:08
@ X-Factor有點:我有2個div:兩者都是絕對位置。上面那個有不同的動態高度,點擊後會改變。下部div的餘量必須根據第一個 – MeltingDog 2013-03-01 07:43:12