Javascript jquery从绑定函数设置变量动画
我使用了一个twitter脚本,我绑定了一个函数来获取加载tweet后div#tweet的动态高度 但之后我需要将该变量传递到一个.animate脚本中-这就是我目前得到的Javascript jquery从绑定函数设置变量动画,javascript,jquery,variables,jquery-animate,Javascript,Jquery,Variables,Jquery Animate,我使用了一个twitter脚本,我绑定了一个函数来获取加载tweet后div#tweet的动态高度 但之后我需要将该变量传递到一个.animate脚本中-这就是我目前得到的 $("#tweet").tweet({ count: 1, username: ["motocomdigital"], loading_text: "searching twitter...", intro_text: null, out
$("#tweet").tweet({
count: 1,
username: ["motocomdigital"],
loading_text: "searching twitter...",
intro_text: null,
outro_text: null
}).bind("loaded", function(){
// this binded function is getting the height of tweet div once loaded...
var tweetHeight = $("#tweet").height();
});
然后我需要将变量tweetHeight传递到下面运行的动画脚本中。我觉得很简单,但我觉得我的编码有点错
$latestTweet.hover(function() {
// see variable in this is first alternation
$latestTweet.stop().animate({ top: "-" + tweetHeight + "px", width: "512px",
}, function() {
$latestTweet.stop().animate({ top: "-1px", width: "334px", backgroundColor: "#464848" }, 300);
});
假设div#tweet height(一旦加载twitter内容)是200px,我需要这个变量的结果是
$latestTweet.stop().animate({ top: "-200px", width: "512px",
有人能帮我理解如何正确地编写它吗?我甚至尝试过将动画脚本放入绑定函数中,但仍然没有传递变量
主要更新
我刚刚弄明白了为什么tweet div的高度不对!这是因为my#tweet div具有顶部和底部填充,并且在创建tweetHeight变量时,.height不包括这些维度,请参阅
有人知道如何将固定像素尺寸添加到我的tweetHeight变量中吗?例如,额外的50像素
我将其绑定到twitter javascript的另一个原因是#tweet div的高度是动态的,因此从顶部位置开始的动画始终需要#tweet div的最终高度才能完美定位。有关简化的演示,请参见。您只需在函数外部创建一个变量:
var tweetHeight = 200; // a default height
// here come's the $.tweet() code, and you should only assign the new value to the "tweetHeight" variable
//$latestTweet.stop().animate({ top: -tweetHeight ...
var tweetHeight = 200;
function setTweetHeight(h) {
tweetHeight = h + 50;
}
更新到上面的“主要更新”
如果使用函数设置变量值,则可以操纵这些额外像素:
var tweetHeight = 200; // a default height
// here come's the $.tweet() code, and you should only assign the new value to the "tweetHeight" variable
//$latestTweet.stop().animate({ top: -tweetHeight ...
var tweetHeight = 200;
function setTweetHeight(h) {
tweetHeight = h + 50;
}
现在使用函数指定新值:
setTweetHeight( $('#tweet').height() ); // example
啊,是的,但是tweet是动态的,我在做jsfiddle时基本上解决了我的问题-它检索div的高度不包括我的填充-你现在知道我如何在我的tweetHeight变量上添加我的顶部和底部填充尺寸了吗?谢谢