简体   繁体   English

ASP页面+ Javascript:如何隐藏元素并获取其真实位置?

[英]asp page + javascript: how to hide an element and get it real position?

I have an javascript, that find an element (panel), gets it position, and scrolls page to that position. 我有一个javascript,可以找到一个元素(面板),获取它的位置,然后将页面滚动到该位置。 The javascript function is called on button click. 单击按钮将调用javascript函数。 everything worsk fine, when the panel is Visible=true; 当面板为Visible = true时,一切正常。 But on the load of the page, the panel must be not visible, to the user. 但是在页面加载时,该面板对于用户必须不可见。 When the panel is Visible = false, the javascript function doesn't works because the panel doesn't exists on the page. 如果面板为Visible = false,则javascript函数不起作用,因为该面板不存在于页面上。 when the panel is style="display:none" it exists, is not visible to the user, but it's position is not it's real position. 当面板为style =“ display:none”时,该面板存在,对用户不可见,但其位置不是实际位置。 when the panel is style="visibility:hidden" the panel exists, is on the real position, but there is a big blank space on the page, where the panel is hidden. 当面板为style =“ visibility:hidden”时,该面板存在于实际位置上,但是页面上有一个很大的空白区域,面板被隐藏。

How to make the panel invisible to the user without the blank space on the page, and get its real position on the page? 如何使面板在页面上没有空白但对用户不可见,并获得其在页面上的真实位置?

edit: my solution + code - 编辑:我的解决方案+代码-

well, I made it working, but there must be better solution. 好吧,我做到了,但是必须有更好的解决方案。 here's the code 这是代码

<script type="text/javascript">
        function elementPosition(obj) {            
            var curleft = 0, curtop = 0;
            if (obj.offsetParent) {
                curleft = obj.offsetLeft;
                curtop = obj.offsetTop;

                while (obj = obj.offsetParent) {
                    curleft += obj.offsetLeft;
                    curtop += obj.offsetTop;
                }
            }
            return { x: curleft, y: curtop };
        }

        function ScrollToControl(id) {
            jid = id;
            var elem = document.getElementById(jid);
//here I set the display = 'block', and I also set this in button_click event
//when I didn't set it also in button_click event, I get the right panel position
//but the panel remains invisible
            elem.style.display= 'block'; 
            var scrollPos = elementPosition(elem).y;
            window.scroll(0, scrollPos);
            }
           </script>

the button on the page: 页面上的按钮:

<asp:Button ID="btnClientDossiers_EMAIL" runat="server" CssClass="button" Enabled="False"
                            Text="EMAIL" OnClick="btnClientDossiers_EMAIL_Click" />

codebehind 代码隐藏

 Page_Load
    {
 //how can I dynamically get the ctl00_ContentPlaceHolder?
// Or will the client name of plEMAIL always start with ctl00_ContentPlaceHolder?
    btnClientDossiers_EMAIL.Attributes.Add("onclick", " return 
    ScrollToControl('ctl00_ContentPlaceHolder_plEMAIL');");
    }

    protected void btnClientDossiers_EMAIL_Click(object sender, EventArgs e)
            {
                plEMAIL.Attributes.Add("style", "display:block");
            }

Please post your code so we can give you a more helpful answer, but I'm going suggest one way that you may be able to achieve what you are after. 请发布您的代码,以便我们为您提供更有用的答案,但是我将建议一种可能使您实现所追求的目标的方法。 Try loading the page with the div(s) set to style="visibility:hidden;" 尝试在div设置为style="visibility:hidden;"加载页面style="visibility:hidden;" , and then get their vertical position into a global variable or a data-attribute of the element, and then immediately set the div's style to style="display:none;" ,然后将其垂直位置放入元素的全局变量或数据属性中,然后立即将div的样式设置为style="display:none;" .

You should then be able to use these position values in your scroll function. 然后,您应该能够在滚动功能中使用这些位置值。

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

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