简体   繁体   English

设置Javascript中剩余的绝对位置不起作用

[英]Setting absolute position left in Javascript not working

I am trying to set my gridview (asp.net) position to absolute when the gridview stretches further than the standard 960 pixels. 当gridview的拉伸程度超过标准960像素时,我试图将自己的gridview(asp.net)位置设置为绝对位置。

However I have run across the issue where left: 0 has the effect I desire, however anything other than that does nothing. 但是,我遇到了left: 0的问题left: 0了我想要的效果,但是除此以外的任何其他操作都不起作用。

Here is my code. 这是我的代码。 The section where it's less than the screen doesnt work because i am trying to center it to the screen. 它小于屏幕的部分不起作用,因为我试图将其居中放置在屏幕上。 However as it's greater than 0 the absolute position of Left: x doesn't work. 但是,因为它大于0,所以Left: x的绝对位置无效。 Any help would be appreciated! 任何帮助,将不胜感激!

 var gridview = document.getElementsByTagName("table");
            var fieldset = document.getElementsByTagName("fieldset");
            fieldset[0].style.position = "relative";
            for (var i = 0; i < gridview.length; i++) {
                var width = gridview[i].offsetWidth;
                var screenWidth = window.screen.width - 2;

                if (width > 940) {
                    gridview[i].style.position = "absolute";
                    if (width > screenWidth) {
                        gridview[i].style.left = 0;
                        alert('> screen');
                    }
                    else {
                        gridview[i].style.left = 50;
                        alert('< screen');
                    }
                }
            }

EDIT: The fix was to concat 'px' after the value 编辑:解决方法是在值后合并'px'

Not really go enough to go on, but it might be the case that the container of gridview hasn't had its position set your css. 进行下去还不够,但是可能是因为gridview容器的位置没有设置您的CSS。

To use the postion:absolute the above container should be set to position:relative or it just wont render the css. 要使用postion:absolute ,应将上述容器设置为position:relative否则它将不会呈现CSS。

Also try adding px to the end of your size, setting the css to left:XXpx; 也可以尝试在大小的末尾添加px,将CSS设置为left:XXpx;。 rather than just a number. 而不是一个数字。

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

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