设置高度动画时元素跳跃
Elements jumping around when animating height
我正在尝试实现一个抽屉,当用户单击手柄时它会向下滑动。页面内容也会被向下推,这样就不会重叠。除了抽屉展开/折叠时div#drawer-content
跳跃外,其他功能都很好。这是一个小跳跃,但我不知道为什么或如何防止它。
工作示例:http://jsfiddle.net/pFsun/
代码:
var InterestBalanceDrawer = (function($, window, document){
var drawer, space, handle, interest_list, body;
var settings = {
content_height: 250,
handle_height: 20,
drawer_height: 30,
drawer_slim_height: 15
};
/**
* Initialize the drawer element handles and call setup functions.
*/
var init = function() {
// We will be moving the body when the drawer expands/collapses
body = $('body');
// Container
drawer = $('<div id="drawer" data-expanded="false"></div>');
// This is the content container of the drawer
content = $('<div id="drawer-content"></div>');
// The button/handle the user clicks on to show/hide the drawer space
handle = $('<div id="drawer-handle">Show</div>');
// The list of interests inside the drawer space
interest_list = $('<ul id="drawer-interest-list"></ul>');
mockCSS();
buildInterestList();
bindUIEvents();
attachToDOM();
}
/**
* Development only. This will be replaced by real CSS
*/
var mockCSS = function() {
drawer.css({
'height': settings.drawer_height,
'width': '100%',
'position': 'relative',
'margin-bottom': '25px'
});
content.css({
'position': 'relative',
'background': '#cccccc',
'height': 0
});
handle.css({
'position': 'absolute',
'height': settings.handle_height,
'bottom': '0',
'padding': '5px',
'background': '#333',
'color': '#fff',
'cursor': 'pointer'
});
}
/**
* Fetches a list of interests and balances. Builds interest_list
*/
var buildInterestList = function() {}
var collapseDrawer = function() {
drawer.animate({
'height': '-='+settings.content_height
});
content.animate({
'height': '-='+(settings.content_height)
});
handle.text(handle.data('label'));
}
var expandDrawer = function() {
drawer.animate({
'height': '+='+settings.content_height
});
content.animate({
'height': '+='+(settings.content_height)
});
handle.text(handle.data('label'));
}
/**
* All event binding should be defined here
*/
var bindUIEvents = function() {
handle.on('click', function(e){
// Flip the data-expanded value
drawer.data('expanded', drawer.data('expanded') === true ? false : true);
// Flip the data-visible value
handle.data('label', drawer.data('expanded') === true ? 'Hide' : 'Show');
if(drawer.data('expanded') === true) {
expandDrawer();
} else {
collapseDrawer();
}
});
}
/**
* Attached everything together and insert in to the DOM
*/
var attachToDOM = function() {
interest_list.appendTo(content);
content.appendTo(drawer);
handle.appendTo(drawer);
var fragment = document.createDocumentFragment();
drawer.appendTo(fragment);
body.prepend(fragment);
}
// Public variables and methods
return {
init: init
}
})(jQuery, window, document)
InterestBalanceDrawer.init();
这与body
或ul
的边距/填充或两者都消失和重新出现有关。
请参阅:http://jsfiddle.net/pFsun/2/
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何设置html元素填充的动画
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 表追加而不附加最后一个元素
- 如何在jQuery中获取元素的形式
- 我可以获得相对于被点击元素的确切点击位置吗
- 在函数中添加数组元素的数值
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 将视口底部滚动到元素底部
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 在可拖动元素中奇怪的跳跃
- 设置高度动画时元素跳跃
- 浮动元素在隐藏另一个浮动元素时会跳跃
- 防止元素在画布中突然跳跃
- CSS列计数跨列跳跃的元素
- 我如何“跳跃”?到一个特定的元素