【问题标题】:Jquery is not working at the time of opening page at first最初打开页面时 Jquery 不工作
【发布时间】:2013-11-26 05:22:06
【问题描述】:

通过使用 jquery,我将 div 放置在窗口的垂直中心。此脚本不支持 $(document).ready(function() 但它仅在窗口调整大小功能后才开始工作。

我需要它在第一次加载页面时工作。参考fiddle

HTML:

<div class="welcomer">
  <center>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </center>
</div>

CSS:

body {
    background: #ccc;
    color:#000;
}
.welcomer {
    width: 100%;
    background: #fff;
}

jQuery

$(document).ready(function(){

$(window).resize(function(){

    $('.welcomer').css({
        position:'absolute',
        "top": ((($(window).height() - $('.welcomer').outerHeight()) / 2) + $(window).scrollTop() + "px"),
        "left": ((($(window).width() - $('.welcomer').outerWidth()) / 2) + $(window).scrollLeft() + "px")
    });


// To initially run the function:
$(window).resize();

});

【问题讨论】:

    标签: jquery dynamic


    【解决方案1】:

    看起来您正在调整大小处理程序中调用 $(window).resize()

    $(document).ready(function () {
        $(window).resize(function () {
            $('.welcomer').css({
                position: 'absolute',
                "top": ((($(window).height() - $('.welcomer').outerHeight()) / 2) + $(window).scrollTop() + "px"),
                "left": ((($(window).width() - $('.welcomer').outerWidth()) / 2) + $(window).scrollLeft() + "px")
            });
    
        }).resize(); // To initially run the function:
    });
    

    【讨论】:

    • 我需要在这个垂直居中对齐的 div 下方创建另一个 div。然后我需要在其中添加这个脚本 var top = $(".welcomer").offset().top;顶部 += $(".welcomer").height(); $(".content_txt").css({ top: top+ "px" });
    猜你喜欢
    • 2016-11-17
    • 2011-08-30
    • 2019-07-18
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多