有没有办法让固定位置的元素在窗口大小调整一定量后停止移动?
我有一个固定位置在窗口右侧的导航栏。但是,如果窗口大小调整为小于特定大小,我希望它停止移动,然后如果窗口大小调整为大于此大小,则将其重新附加到右侧。
我正在尝试使用 jquery 和 $(window).resize ,并且能够在某个点之后阻止元素向内移动,但如果窗口缩放得更大,它不会重新附加。
此外,效果不是很流畅并且相当刺耳。
任何其他解决方案或想法将不胜感激!
$(window).resize(function(){
var windowWidth = $(window).width();
if (windowWidth < 1200) {
$('#test').css('left', '1100px');
}
else {
$('#test').css('right', '55px');
}
});
I have a navigation bar that is fixed-position to the right side of the window. However, I want it to stop moving if the window is resized smaller than a certain size, and then reattach itself to the right side if the window is resized larger than this size.
I am trying to use jquery and $(window).resize and am able to stop the element from moving inwards after a certain point, but it does not reattach if the window is scaled larger.
Also, the effect is not very fluid and is rather jarring.
Any other solutions or ideas would be greatly appreciated!
$(window).resize(function(){
var windowWidth = $(window).width();
if (windowWidth < 1200) {
$('#test').css('left', '1100px');
}
else {
$('#test').css('right', '55px');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于此类功能,您可以使用媒体查询
编写,如下所示:
阅读这些文章更多
http://css-tricks.com/6731-css-media-queries/,
For this type of functionality you can use Media Query
write like:
Read these article more
http://css-tricks.com/6731-css-media-queries/ ,
http://css-tricks.com/6206-resolution-specific-stylesheets/
一旦您分配了左右距离,它就会保留它们。
当您设置正确的距离时,将 left 设置为 auto,它应该可以
编辑(由匿名用户添加。)
Stratton 是完全正确的,您为“left”分配一个值,并且永远不会将其重新分配为 auto。如果你在 else 语句上这样做,你就会让它工作。尝试使用这段代码,您将获得一种效果,就像它只是移动到某个点一样。
关于性能,很大程度上取决于您使用的计算机和浏览器,例如在我的计算机中的 Chrome 上看起来很棒,但在 Opera 中则不然。这可能是因为(例如)Opera 仅当您停止调整事件大小时才会触发该事件 (这篇文章对此进行了一些解释)。
It keeps both distances, left and right once you've assigned them.
Set left to auto when you set the right distance and it should work
EDIT (Addition by anonymous user.)
Stratton is completely right, you assign a value to "left" and never reassign it to auto. if you do it on else statement, you will have it working. Try with this code, and you'll have an effect like it's only moving passed some point.
About performance, depends a lot of the computer and browser you use, for example on chrome in my computer looks great, but not in opera. That could be because (for example) Opera fires the event only when you stop resizing it (this article explains it a little bit).
使用绝对定位而不是固定定位。放置一个具有相对定位的包装 div,然后给它最小宽度值。将导航栏放在具有绝对定位的包装内部。
Use absolute instead of fixed positioning.. Put a wrapper div with relative positioning then give it min-width value.. Put your navigation bar inside of wrapper with absolute positioning..