Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine where the current visible vertical location inside a jquery dialog is?

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")
        });
like image 850
leora Avatar asked Apr 24 '14 13:04

leora


2 Answers

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

like image 138
Dave Alperovich Avatar answered Oct 11 '22 12:10

Dave Alperovich


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',
                complete: 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;
    ...
}
like image 24
TheGr8_Nik Avatar answered Oct 11 '22 12:10

TheGr8_Nik