jQuery 动态偏移
所以我一直在研究这个 http://jsfiddle.net/Hiilo/hCVf4/
我会做什么想要实现的是 #offy 的偏移量将等于手风琴高度,但我只是不知道如何更新 offset.top 值。
这里设法在页面加载时给出正确的值,但如果我单击手风琴标题,则不会更新。
var offset= function() {
var offsetValue = $("#accordion").height();
return offsetValue;
};
现在,如果我查看控制台,它似乎已更新,
$(".ui-accordion-header").click(function(){
console.log(offset());
});
我尝试在单击函数内设置变量偏移量,但随后我无法在函数外获取更新后的值。 现在我知道这与变量作用域和闭包有关(一直在阅读它们),但我仍然一无所知。
提前致谢
So i have been working on this http://jsfiddle.net/Hiilo/hCVf4/
What i would like to achieve is that the offset of #offy would be equal to the accordion height, but i just can't figure out how to update the offset.top value.
This here manages to give the correct value on page load but wont be updated if i click on the accordion header.
var offset= function() {
var offsetValue = $("#accordion").height();
return offsetValue;
};
Now if I look in to the console it seems to be updated
$(".ui-accordion-header").click(function(){
console.log(offset());
});
I have tried to set the variable offset inside the click function but then I cant get the updated value outside the function.
Now i know that this has everything to do with variable scope and closure(been reading up about them) but i'm still left in the dark.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找这个: http://jsfiddle.net/hCVf4/13/
更改从标题单击到更改回调的处理程序,因为单击手风琴仍未完成动画,高度将无效:
将块的样式更改为
position:absolute ; left:300px;
css(top:myTopValue)
You are looing for this: http://jsfiddle.net/hCVf4/13/
Change the handler from header click to the change callback, because on click accordion still not finished animation and height will be invalid:
Change the style of the block to the
position:absolute; left:300px;
css(top:myTopValue)
on the callback