Use jQuery to show a div in 5 seconds

2020-06-27 00:33发布

I want to fade in a div on my website in 5 seconds. Also, I dont want to use css Display:none to hide the div, because this div is very important and I'm thinking if the user doesnt have JS enabled, the div will be hidden forever. So can you guys please tell me how to hide the div on website load and make it visible in 5 seconds? Thanks heaps.

<div id="lead_form"></div>

标签: jquery
7条回答
迷人小祖宗
2楼-- · 2020-06-27 00:57

first you should hide $('#load_form') before appending to page : $('#load_form').hide() then you should show it with this function .fadeIn( [duration] [, callback] ) you must set duration 5000.

查看更多
Emotional °昔
3楼-- · 2020-06-27 00:58

Use noscript tag and put div in it to display when java script is disabled in browser.

<noscript>
    <div id="lead_form"></div>
</noscript>

Use below code to fade in with in five seconds for java script enabled browsers

$("#lead_form").fadeIn(5000);
查看更多
欢心
4楼-- · 2020-06-27 00:59

As you don't want to hide the div forever if JS is disabled, you'd want to hide it on doc ready and then show it again in 5 seconds. Here's a fiddle that demonstrates the behavior. However, if the div is important enough to be shown all the time with JS off, then I'm not sure if you'd really want to hide it and then show it in the first place.

查看更多
家丑人穷心不美
5楼-- · 2020-06-27 01:11

If you want to make a unobstrusive feature, you should let the div visible by default.

When the page is correctly loaded, the first task to do is to hide the DIV with jQuery, and then, run an animation to show it in 5s.

By doing this way, if there is no JS activated, the DIV is already available.

$(document).ready(function() {

    // Hide the div
    $("#lead_form").hide();

    // Show the div in 5s
    $("#lead_form").delay(5000).fadeIn(500);

});
查看更多
家丑人穷心不美
6楼-- · 2020-06-27 01:16
setTimeout(function(){
   $('#lead_form').show();// or fade, css display however you'd like.
}, 5000);
查看更多
Summer. ? 凉城
7楼-- · 2020-06-27 01:16

Try this

 $('#lead_form').hide("fast",function(){
    $("#lead_form").show(5000);
    });
查看更多
登录 后发表回答