jQuery设置的边距顶部使内容跳跃.如何使其平滑动画化
margin-top set by jQuery makes content jump. How can I make it smoothly animate?
#content
div通过jQuery获取margin-top
集。此边距与没有设置高度的 #masthead
div 高度相同,因为它会发生变化。这一切都很好用。问题是当我重新加载页面时,由于 jQuery 设置的边距,#content
div 会"跳"下来。在小提琴中可能很难看到它,但我已经设法重现了我正在经历的事情。如何为边距设置动画#content
以便平滑地向下移动?
小提琴:http://jsfiddle.net/78a55ctm/
.HTML
<div id="masthead">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
.CSS
* { margin:0;padding:0 }
#masthead {
background: #000;
color: #000;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
#content {
background: pink;
color: #fff;
padding: 10px;
}
.JS
var headerTop = $('#masthead').outerHeight();
$('#content').css('margin-top', headerTop);
您可以使用animate()
$('#content').animate({'margin-top': headerTop + 'px'}, 700, 'easeOutExpo');
查看演示
试试这段代码
var headerTop = $('#masthead').outerHeight();
$('#content').animate({ marginTop: headerTop }, 1000);
相关文章:
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 如何重写下面的函数,使其不会't用于循环
- Ember选择绑定-如何使其着火
- 动态修改一个元素,使其与给定的选择器匹配
- 如何使用javascript操作SVG节点,使其在MS Edge中工作
- Javascript将数组转换为字符串并使其反向输出
- 如何添加两个变量而不使其在javascript中连接
- Chrome内容脚本制作cookie;chrome重新启动时,cookie会被删除.如何使其稳定
- 如何进行AJAX调用,使其仅在我单击包含在特定类中的链接时触发;id”;父元素的
- 选择一个元素,使其最近的具有特殊属性的父元素具有特殊值
- 缓存事件的变量而不使其全局化
- angularjs文本区域-如何使其与angularjs下拉列表中的选定值保持同步
- 当我在平板电脑中关闭应用程序时,如何使其保持会话有效
- 扩展在LeafletJS中是如何工作的,为什么我需要一个中介归因才能使其正确工作
- 如何在拖动对象时检测画布的中心线以使其居中
- 如何缩小大图像 (1600x1200) 的大小,使其适合大多数屏幕作为背景
- 必须对 JavaScript 进行哪些更改才能使其可编译为二进制
- 有没有办法覆盖 setTimeout 函数,使其使用微秒而不是毫秒
- jQuery设置的边距顶部使内容跳跃.如何使其平滑动画化
- 如何使计时器每秒挠痒痒,并在转发或倒带视频时使其跳跃