简体   繁体   English

如何在jQuery中获得div的最高价值

[英]How to get the Top value of a div in jQuery

This is a silly problem but a problem none the less. 这是一个愚蠢的问题,但仍然是一个问题。 I'm simply looking to get the "Top" value of a panel after it has appeared on the screen. 我只是想在面板出现在屏幕上之后获取面板的“顶部”值。 So when I go to a page as the panel renders in its position I want to get its top value. 因此,当我在面板呈现其位置时转到页面时,我想获得其最高价值。 Here is the code I'm using: 这是我正在使用的代码:

//signinput is the panel whose top value I want    
$("#signinput").bind('afterShow', function() 
    {
        TopVal = $("#signinput").position().top;
        alert(TopVal);
        TopVal = TopVal+"px";
    }); 

But some how this doesn't even alert anything. 但是有些方法甚至都不会发出任何警告。 So for some reason this function I'm binding isn't event running for some reason. 因此由于某种原因,我绑定的此函数由于某种原因而没有运行事件。 Any suggestions? 有什么建议么?

Here is my HTML code: 这是我的HTML代码:

<div id="signinput">
              <div class="panel">
                <center>
                    <h3>Sign Into Blah Blah</h3>

                        <form name="SignIn" action="#" method="post">
                            <input type="text" name="signinemail" id="signinemail" placeholder="Enter Email">
                            <input type="password" name="signinpassword" id="signinpassword" placeholder="Enter Password">
                            <input type="submit" id ="signinsubmit" name="signinsubmit" value="Sign In" data-theme="a">
                        </form>
                </center>
              </div>
            </div>

This function returns the top value if you are not passing a value: 如果不传递值,则此函数返回最高值:

var TopVal = $("signinput").scrollTop();

Edit: By the way you said "after appearing on screen", can it be that you are missing to use jquery.live to search for the element in an updated document? 编辑:用您所说的“出现在屏幕上之后”的方式,是否可能是您缺少使用jquery.live在更新的文档中搜索元素? Otherwise your JQuery selector $("singinput") would be NULL: 否则,您的JQuery选择器$(“ singinput”)将为NULL:

Use: 采用:

$("#signinput").live("afterShow", function() 

////Some additional info: ////一些其他信息:

And when you pass a numeric parameter to the function "scrollTop" instead you can modify it and force the browser to scroll this container. 而且,当您将数字参数传递给函数“ scrollTop”时,可以对其进行修改并强制浏览器滚动此容器。

See: 看到:

http://api.jquery.com/scrolltop/ http://api.jquery.com/scrolltop/

well, if afterShow is you coustom event then you need to trigger it in document.ready function 好吧,如果afterShow是您的事件,那么您需要在document.ready函数中触发它

try this 尝试这个

  $(function (){
    $("#signinput").bind('afterShow', function() 
    {
       TopVal = $("#signinput").position().top;
       alert(TopVal);
       TopVal = TopVal+"px";
    });
     $('#signinput').trigger('afterShow'); 
 });

i am not sure , why are you using that custom event of yours. 我不确定,您为什么要使用您的自定义事件。 calling top inside document.ready should work (assuming you just have an alert inside the afterShow event). 在document.ready内部调用top应该可以工作(假设您在afterShow事件内部只有一个警报)。

  $(function (){

     TopVal = $("#signinput").position().top;
       alert(TopVal);
       TopVal = TopVal+"px"; 
 });

There is no afterShow event, and a div has no load event as it's rendered immediately, so the only thing you might need is a DOM ready handler : 没有afterShow事件,并且div没有立即加载的load事件,因此您唯一需要的就是DOM ready处理程序:

$(function() {
    TopVal = $("#signinput").position().top;
    alert(TopVal);
    TopVal = TopVal+"px";
}); 

I don't know which event will "afterShow" touch off, but after trying, I found I couldn't trap into this trigger when the element with this trigger showed. 我不知道哪个事件将“ afterShow”触发,但是尝试之后,我发现当显示带有此触发器的元素时,我无法陷入该触发器。

I think adeneo's answer may be the solution of your question. 我认为adeneo的答案可能是您问题的解决方案。

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

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