Search code examples
javascriptjqueryjquery-uijquery-dialog

How to Scroll Flow JQuery UI Dialog


I have been trying to use follow scroll to move dialog together with user scroll but no success

<script>
$(function() {
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog-report-problem-form" ).dialog({
        autoOpen: true,
        height: 550,
        width: 700,
        modal: true,
        buttons: {
            "<?= $this->translate('REPORT_PROBLEM'); ?>": function() {
                reportProblem();
            },
            "<?= $this->translate('CANCEL'); ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
        }
    });
    $.scrollFollow("#dialog-report-problem-form",{speed: 10}); 
});
</script>

.

<div id="dialog-report-problem-form" title="<?= $this->translate('REPORT_PROBLEM'); ?>">
    <?= $this->form ?>
</div>

I have been receiving the error

 box.cont.offset() is null

Does anyone knows how could fix or another jquery based solution to follow user scroll?


Solution

  • The plugin scrollFollow seems to be pretty buggy and development discontinued (last update in 2008)

    • when you use it with $.scrollFollow(), the default option values are not set so you get a lot of errors like the one you got.
    • when using it with $(...).scrollFollow, the main option container is not obtained correctly so it does not really work...

    Here is a small script that will move the dialog around when the window is scrolled:

    (function(wnd, $) {
    
            // query for elements once
        var $dlg = $("#dialog-report-problem-form").parent(),
            $wnd = $(wnd),
            // get the initial position of dialog
            initialTop = $dlg.offset().top - $wnd.scrollTop();
    
        $wnd.scroll(function() {
                // when qscrolling, animate the 'top' property
                $dlg.stop()
                    .animate({
                        "top": ($wnd.scrollTop() + initialTop) + "px"
                    }, "slow");
            })
            .resize(function() {
                // in case of resize, re-set the initial top position of the dialog
                initialTop =  $dlg.offset().top - $wnd.scrollTop();
            });
    
        // if you close/open the dialog, it will mess up the 'initialTop'
        // this will re-set the correct 'initialTop' when the dialog opens again
        $dlg.bind('dialogcreate dialogopen', function(e) {
            initialTop = $dlg.offset().top - $wnd.scrollTop();
        });
    
    })(window, jQuery);
    

    Working example on jsfiddle.