jQuery hide() div until fully loaded

狂风中的少年 提交于 2019-12-06 06:20:16

问题


I'm using tabbed featured post for my blog. How to implement the div#latest-featured will hide() then show() it back after content fully loaded?

$(document).ready(function() {
    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });
});

HTML

<ul class="tabs">
  <li><a href="#f1">Title 1</a></li>
  <li><a href="#f2">Title 2</a></li>
</ul>
<div id="latest-featured">
  <div id="f1" class="tab_content">
    <p>Content for title 1</p>
    <p>Why I want to hide the #latest-featured because.. when had image @ script here.. the tab_content will collapse all and break my design</p>
  </div>
  <div id="f2" class="tab_content">
    <p>Content for title 1</p>
    <p>Why I want to hide the #latest-featured because.. when had image @ script here.. the tab_content will collapse all and break my design</p>
  </div>
</div>

Example I have a div with id #latest-featured and I want hide it until the content is fully loaded, and then show it back after everything is loaded.

How to implement to the current code above.


回答1:


Probably best to do something like this:

<div id="latest-features" style="display:none;"></div>

$(function() {
    // do work
    $("div#latest-featured").show();
}

Its usually not a good idea to hide anything by default but this will meet your requirement.




回答2:


If you want it to show once loaded declare the CSS display: none for that div, and then call $('div#latest-featured').show(); in your $(document).ready function.




回答3:


If you want it to show once loaded declare the CSS display: none for that div, and then call $('div#latest-featured').show(); in your $(document).ready function.

Robert answered it best I would vote on it but can't yet...looked for this solution for hours and this was the best I have found...




回答4:


Why not just set the div to display:none until you have loaded the data, then remove the display:none ... there's lots of ways to do what I just mentioned btw (for instance, jQuery .toggle(true) )



来源:https://stackoverflow.com/questions/3452226/jquery-hide-div-until-fully-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!