te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to determine where the current visible vertical location inside a jquery dialog is? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to determine where the current visible vertical location inside a jquery dialog is? - Stack Overflow

programmeradmin3浏览0评论

I have a case where I am using a jquery ui dialog and I have any html table in the dialog where the dialog is fixed height:

$("#modalDialogContainer").dialog({
    resizable: false,
    height: 700,
    autoOpen: false,
    width: 1050,
    modal: true,

I call an AJAX query from a button click and I want to use jquery UI blockUI plugin to show a "loading" message. Something like this:

   $("#myTableInsideDialog").block({
                css: {
                    top: '200px',
                    bottom: "",
                    left: ''
                },
                centerY: false, baseZ: 2000, message: $("#SavingMessage")
            });

The issue I have is that the content in the dialog is longer than the height of the dialog and I given the dialog is FIXED height so that causes the dialog to have a vertical scroll bar.

Having the scroll bar is fine (that's actually what I want) but the knock on effect is that because of that depending if the user has scrolled down or not, the blockUI message is not centered (or even visible on the screen) vertically.

Question: Is there anyway I can detect what is visible areas inside a dialog that has a vertical scroll bar to vertically align the block message properly?

Above as you can see its hard coded to be 200px from the top so it works great if the user hasn't scrolled down but you can't see the message if the user has scrolled down the whole way

In short, if i am at the top of the scroll, then i would have this:

$("#myTableInsideDialog").block({
            css: {
                top: '200px',
                bottom: "",
                left: ''
            },
            centerY: false, baseZ: 2000, message: $("#SavingMessage")
        });

if i am at the bottom of the scroll, then i would want this:

 $("#myTableInsideDialog").block({
            css: {
                top: '',
                bottom: "200px",
                left: ''
            },
            centerY: false, baseZ: 2000, message: $("#SavingMessage")
        });

I have a case where I am using a jquery ui dialog and I have any html table in the dialog where the dialog is fixed height:

$("#modalDialogContainer").dialog({
    resizable: false,
    height: 700,
    autoOpen: false,
    width: 1050,
    modal: true,

I call an AJAX query from a button click and I want to use jquery UI blockUI plugin to show a "loading" message. Something like this:

   $("#myTableInsideDialog").block({
                css: {
                    top: '200px',
                    bottom: "",
                    left: ''
                },
                centerY: false, baseZ: 2000, message: $("#SavingMessage")
            });

The issue I have is that the content in the dialog is longer than the height of the dialog and I given the dialog is FIXED height so that causes the dialog to have a vertical scroll bar.

Having the scroll bar is fine (that's actually what I want) but the knock on effect is that because of that depending if the user has scrolled down or not, the blockUI message is not centered (or even visible on the screen) vertically.

Question: Is there anyway I can detect what is visible areas inside a dialog that has a vertical scroll bar to vertically align the block message properly?

Above as you can see its hard coded to be 200px from the top so it works great if the user hasn't scrolled down but you can't see the message if the user has scrolled down the whole way

In short, if i am at the top of the scroll, then i would have this:

$("#myTableInsideDialog").block({
            css: {
                top: '200px',
                bottom: "",
                left: ''
            },
            centerY: false, baseZ: 2000, message: $("#SavingMessage")
        });

if i am at the bottom of the scroll, then i would want this:

 $("#myTableInsideDialog").block({
            css: {
                top: '',
                bottom: "200px",
                left: ''
            },
            centerY: false, baseZ: 2000, message: $("#SavingMessage")
        });
Share Improve this question edited May 10, 2014 at 11:06 MKaama 1,9392 gold badges20 silver badges30 bronze badges asked Apr 24, 2014 at 13:48 leoraleora 197k367 gold badges906 silver badges1.4k bronze badges 3
  • Does the message need to be contained within the modal? Can you replicate in jsfiddle to demonstrate? – Patrick Commented May 7, 2014 at 8:10
  • The question title and content are asking for two different things. Position determination is one thing and message positioning is another, best achieved without determining anything as in TheGr8_Nik's answer. – MKaama Commented May 10, 2014 at 11:10
  • @MKaama, it's better to understand the OP's intent and needs BEFORE crusading. The solution you love so much involves DOM manipulation, near the top of things to avoid if possible. You are championing this cause over a simple scroll location calculation. – Dave Alperovich Commented May 10, 2014 at 14:20
Add a ment  | 

5 Answers 5

Reset to default 8 +125

I wouldn't alternate between top AND bottom properties:

For a window sized 1000px, top:800 == bottom:200

The important question, is how you can find out your scroll distance from the top. For that lets use a function:

function calcTopLocal() {
        var s = $('#modalDialogContainer').scrollTop() + 'px';
        return s;
}

Now, to apply it to your block:

 $("#myTableInsideDialog").block({
            css: {
                top: calcTopLocal()
            },
            centerY: false, baseZ: 2000, message: $("#SavingMessage")
        });

This can be refactored many ways. The significant detail is using scrollTop() and applying styling.


response to MKaama:

My proposed answer has no loops, no timers, and no suggestions of repeated action. There is no

Repeatedly calling a js function just to keep the position fixed is an overkill, a waste of CPU

If you want to add an loading message when the ajax is requesting the data, you can append a <div> on the dialog containing the message you want to display. Then you can apply a relative position to the dialog and an absolute position to the <div> and with margin:auto the div remains in the center of dialog always, even if you scroll the dialog.

jsFiddle demo

$("#modalDialogContainer").dialog({
    resizable: true,
    height: 300,
    autoOpen: true,
    width: 300,
    modal: true,
    buttons: {
        'call ajax': function(){
            // insert the loading div to the dialog
            $(this).parent().append("<div class='loading' />");

            $.ajax({
                type: 'json',
                url:  'jsonRequest.php',
                plete: function(){
                    // remove the loading div
                    $('.loading').remove();    
                },
                success: function(){
                    //do what you want
                }
            });
        }
    }
});

the CSS file should be something like this

#modalDialogContainer{
    position: relative;
}

#myTableInsideDialog{
    height: 1000px;
    width:  100%;
}

.loading{
    position: absolute;
    top:      0px;
    bottom:   0px;
    left:     0px;
    right:    0px;
    margin:   auto;
    ...
}

there is a useful plugin that can tell if an element is visile on screen or not ( scrolled to ) , you may simply use it , the function returns true for visible areas on screen :

Here is a quick demo:

http://opensource.teamdf./visible/examples/demo-basic.html

Here is the source page :

http://www.teamdf./web/194/jquery-element-onscreen-visibility

usage as simple as:

$('#element').visible()

Use

$('#modalDialogContainer').scrollTop() 

to find the amount of user's scroll.

You can then show your message with

 { top: $('#modalDialogContainer').scrollTop()+'px' }

And it will always be visible for them, and appear at the top of what they are looking at :)

Why bother with the height of the content at all?

I mean, isn't an easier solution to the problem possible by putting a "BlockUI" on the JQuery Dialog. Since you have a fixed height there, your block UI would most certainly be fixed as well. There is no way the scroll can now affect your message.

A crude example is hosted here in fiddle. It gives you both experiences so you can see how it behaves.

For example, you can put the block UI on the following class.

var container = ".ui-dialog";
$(container).block({
   message: '<h1>Processing</h1>' 
});
$.ajax({
    url: "/echo/json/",
    data: {
        json: {},
        delay: 5
    }
}).done(function() {
   console.log("Done with ajax");
    $(container).unblock();
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论