2014-10-03 74 views
0

我想在此示例中使用一个简单的模式窗口http://jqueryui.com/dialog/,以便在页面主体在特定位置向下滚动时向上浮动。它可以是右上角,右下角或中间右侧。使用jQuery浮动模态窗口

我用他的代码:

$(function() { 

    $("#dialog").dialog({ 
     maxHeight: 200, 
     width: 400, 
     autoOpen: true, 
     show: { 
      effect: "blind", 
      duration: 1000 
     }, 
     hide: { 
      effect: "explode", 
      duration: 1000 
     } 
    }); 
    $("#opener").click(function() { 
     $("#dialog").dialog("open"); 
    }); 

    $(window).scroll(function() { 

     //after window scroll fire it will add define pixel added to that element. 
     set = $(document).scrollTop() + "px"; 

     //this is the jQuery animate function to fixed the div position after scrolling. 
     $('#dialog').animate({ top: set }, { duration: 1000, queue: false }); 


    }); 
}); 

怎么过,这只会浮在对话而不是整个窗口内的文本。

任何帮助将非常理解:)

+0

请让一个jsfiddle并把代码。 – 2014-10-03 16:41:51

+0

我试过,但没有工作... html代码是一样的http://jqueryui.com/dialog/ – Aslan 2014-10-03 16:42:42

+0

你的位置:绝对'css? – Ohgodwhy 2014-10-03 16:46:30

回答

1

尝试“#dialog”选择更改为“的.ui-对话框”

$(window).scroll(function() { 

    //after window scroll fire it will add define pixel added to that element. 
    set = $(document).scrollTop() + "px"; 

    //this is the jQuery animate function to fixed the div position after scrolling. 
    $('.ui-dialog').animate({ top: set }, { duration: 1000, queue: false }); 
}); 

因为$('#对话')一旦对话框由jQuery UI创建,就会转换为新的DOM元素,并且新的DOM元素可以通过$('。ui-dialog')

+1

这是正确的。旁注:男人,持续时间1000,这将是一个丑陋的过渡。哦,它不会转换成一个新的DOM元素,如果那样的话事件将不得不被反弹导致性能下降,而是调用'.wrap()'。 – Ohgodwhy 2014-10-03 16:51:23

+1

@Ohgodwhy谢谢澄清 – madushak 2014-10-03 16:54:00

+0

谢谢@madushak! – Aslan 2014-10-03 17:56:08