草庐IT

javascript - Dynamicpage div 在加载新内容后不久闪烁

coder 2025-01-18 原文

我正在使用动态页面脚本通过 Ajax 将内容加载到我的页面中。 除了 1 期外,一切正常。 我有一个页面,其中的 div 彼此堆叠在一起 例如:header-banner-nav-content-likebox-twitterwidget-footer。

如果我更改页面,例如,如果我从家里去联系,然后我会看到我的 likebox 和 twitterwidget 在不到一秒钟的时间内转到页面顶部(导航下方)。之后一切都正确加载,但是在显示联系页面之前看到框在顶部闪烁很烦人。 如何修改 dynamicpage.js 脚本,使其不会很快将内容下方的 div 移动到顶部?

这是我的脚本:

$(function() {

    var newHash      = "",
        $mainContent = $("#main-content"),
        $pageWrap    = $("#page=wrap"),
        baseHeight   = 0,
        $el;

    $pageWrap.height($pageWrap.height());
    baseHeight = $pageWrap.height() - $mainContent.height();

    $("nav").delegate("a", "click", function() {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function(){ 
String.prototype.toTitleCase = function(n) {
   var s = this;
   if (1 !== n) s = s.toLowerCase();
   return s.replace(/\b[a-z]/g,function(f){return f.toUpperCase()});
   }

   newHash = window.location.hash.substring(1);         

   function changeTitle(title) { 

   document.title = window.location.hash.replace("#","").replace(/[_]/g,"").replace(".html","").replace("and","+").toTitleCase(); }
   changeTitle("");

        newHash = window.location.hash.substring(1);

        if (newHash) {
            $mainContent
                .find("#guts")
                .fadeOut(200, function() {
                    $mainContent.hide().load(newHash + " #guts", function() {
                        $mainContent.fadeIn(200, function() {
                            $pageWrap.animate({
                                height: baseHeight + $mainContent.height() + "px"
                            });
                        });
                        $("nav a").removeClass("current");
                        $("nav a[href="+newHash+"]").addClass("current");
                    });
                });
        };

    });
    $(window).trigger('hashchange');



});

这是我的CSS:

.header-wrapper {
    display:block;
    clear: none;
    max-width:960px;
    margin:auto;

}
header {
    position:relative;
    display: block;
    width: 100%;
    max-width:960px;
    min-height: 110px;
    background: url('../img/header-d.gif') no-repeat;
    margin:0 auto;
    margin-top:15px;
}
.like-button {
    position:absolute;
    right: 36%;
    bottom:0;
}
.like-button-twitter {
    position:absolute;
    right:22%;
    bottom:2px;
}
#email {
     width: 180px; 
     background: #FFFFFF; 
     border: 1px solid #BBBBBB;
     position:absolute;
     right:2px;
     bottom:5px;
     margin: 0 5px 0 0;
     padding: 4px;  
     font-size: 10px;
     }  
.go {
    position: absolute;
    right:8px;
    bottom:10px;
    border:none;
    color: #455868;
    font-weight:bold;

}
#icons {
    background: url('../img/icons.png') no-repeat;
    position: absolute;
    list-style:none;
    width:210px;
    height:35px;
    right:0;

    }
#icons li {
    position:absolute;
    list-style:none;
    }
li icon1 {
    position:absolute;
    right:100px;
    width:35px;

}
nav {
    min-height: 40px;
    width: 100%; 
    background: #374652 ; /*#455868 */
    font-size: 10pt;
    font-family: sans-serif;
    position: relative;
    border-bottom: 2px solid #283744;
}
nav ul {
    padding: 0;
    margin: 0 auto;
    width: 400px;
    height: 20px;
}
nav li {
    display: inline;
    float: left;
}
nav a {
    display: inline-block;
    width: 100px;
    text-align: center;
    text-decoration: none;
    line-height: 40px;
    color:#ffffff;
}
nav li a {
    border-right: 1px solid #576979;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    color:#ffffff;
}
nav a:visited
{   color:#ffffff; }

nav li:last-child a {
    border-right: 0;
}
nav a:hover, nav a:active {
    background-color: #2d3a44;
}
nav a#pull {
    display: none;
}
#banner {
    clear: both;
    float: left;
    margin-left: 0;
    width: 100%;
    display:block;  
    }
.wrapper {
    width: 100%;
    max-width: 960px;
    margin: auto;
}
article {
    clear: both;
    float: left;
    margin-left: 0;
    margin-top:30px;
    width: 62%;
    display: block;
}

#youtube-music .container {height:450px;}

section {
    padding-left:8px;
}
aside {
    clear: none;
    float: right;
    margin-left: 3%;
    padding-right:1%;
    margin-top:30px;
    width: 33.2033%;
    display: block;
}
#like-box {
    clear: none;
    float: left;
    width: 100%;
    display: block;
    overflow:hidden;
    border:1px solid #2c2c2c;
}
.fb-like-box  {
    width: 100% !important;
    border:none; 
    overflow:hidden;

}
#youtube-box {
    clear: none;
    float: left;
    width: 100%;
    margin-top:10px;
    display: block;
    border:1px solid #2c2c2c;
}
#twitter-box {
    clear: none;
    float: left;
    width: 100%;
    margin-top:10px;
    display: block;
    overflow:hidden;
    border:1px solid #2c2c2c;
}
#twitter {
width:100%;
}

#twitter_m {
width: 100%;
padding: 0 13px;
}
#twitter_container {
min-height:45px;
height:auto !important;

}
#twitter_update_list {
width: 100%;
padding: 0;
overflow: hidden;
font-size: 14px;
color: #374652;
line-height: 16px;
margin-left:-13px;
}
#twitter_update_list li {
width: 90%;
padding:10px;
border-bottom: solid 1px #ccc;
}
#twitter_update_list li a {
color:#631891;
text-decoration: none;
}
#twitter_update_list li a:hover {
color: #31353d;
}

footer { margin-top: 15px; display:block; background:#374652; color: #d5d5d5;}
#footer-container {position: relative; height: 250px; max-width:960px; margin:auto;}
footer ul { padding-top: 5px; padding-left:15px;}
footer ul li{float:left; position: relative; padding-right:15px;  display:inline;}
footer ul li a{color: #d5d5d5;}
footer ul li a:hover{color: #777;}
#footer-container p { position: absolute; bottom:30px;}

#youtube-music {    
    clear: none;
    float: left;
    width: 100%;
    display: block;
    }

最佳答案

您的代码可能在所有样式完全加载之前就已运行。您可以尝试使用 .load() 而不是 $(function(){...}) 来调用您的代码

    $(window).load(function () {
      // run code
    });

这将导致您的代码在窗口完全加载后运行。

关于javascript - Dynamicpage div 在加载新内容后不久闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12187249/

有关javascript - Dynamicpage div 在加载新内容后不久闪烁的更多相关文章

  1. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  2. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  5. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  6. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  7. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

  8. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  9. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  10. ruby - 如何在ruby中提取方括号内的内容 - 2

    我正在尝试提取方括号内的内容。到目前为止,我一直在使用它,它有效,但我想知道我是否可以直接在正则表达式中使用某些东西,而不是使用这个删除功能。a="Thisissuchagreatday[coolawesome]"a[/\[.*?\]/].delete('[]')#=>"coolawesome" 最佳答案 差不多。a="Thisissuchagreatday[coolawesome]"a[/\[(.*?)\]/,1]#=>"coolawesome"a[/(?"coolawesome"第一个依赖于提取组而不是完全匹配;第二个利用前瞻和

随机推荐