简体   繁体   English

用Javascript更新(style.top)浮动菜单的位置

[英]Updating (style.top) position of a floating menu with Javascript

I'm doing a floating menu with JavaScript, is suppose to stay in the same position no mater if the user scrolls down or resize the window. 我正在使用JavaScript进行浮动菜单设置,假设用户向下滚动或调整窗口大小时,该菜单将保持不变。

So far it is working just fine with X position. 到目前为止,它在X位置工作正常。 However, I can't find a way to make it stick to a position relative to the visible top of the window. 但是,我找不到使它固定在相对于窗口顶部可见的位置的方法。 The problem is when I scroll down the menu disappear because it keeps its distance related to the window size. 问题是当我向下滚动菜单时消失,因为它保持与窗口大小有关的距离。

my code is: 我的代码是:

    menuPosition : function (){
        var windowHeight = document.body.clientHeight;
        var windowWidth = document.body.clientWidth;
        var xPosFloatingMenu = (windowWidth) - (fMenuWidthGlobal + fMenuXPosGlobal);
        var yPosFloatingMenu = (windowHeight) - (windowHeight - fMenuYPosGlobal);
        document.getElementById("floating_menu").style.left = (xPosFloatingMenu) + "px";
        document.getElementById("floating_menu").style.top = (yPosFloatingMenu) + "px";


    },

    updateMenuPosition : function () {
        var menuPositionInterval = setInterval(gala.create.menuPosition,1);
    } 

Is there a way to keep the menu position update relative to the visible top of the window? 有没有办法使菜单位置相对于窗口的可见顶部保持更新?

You have to update the style properties of your floating_menu element on the window scroll Event . 您必须在窗口滚动Event上更新float_menu元素的样式属性。

https://developer.mozilla.org/en-US/docs/Web/API/window.onscroll https://developer.mozilla.org/en-US/docs/Web/API/window.onscroll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM