在页面加载

时间:2015-09-17 07:09:23

标签: javascript jquery html css

在页面加载时,我想淡入一个CSS id然后几秒后我想淡入另一个CSS id。有人知道我会用什么jquery代码吗?

这是两个id

#background {
background: url("images/am_photography_bg.jpg") no-repeat center -25px fixed;
background-size: 100%;
height: 100vh;
}

#background-blured {
background: url("images/am_photography_bg_blured.jpg") no-repeat center -25px fixed;
background-size: 100%;
height: 100vh;
opacity: .5;
}

这是我的HTML

<html>
<head>
</head>
<body>
<section>
    <div id="background"></div>
</section>
</html>

有什么想法吗?

我尝试将此代码添加到现有的jQuery中:

    $("#background").delay(1500).animate({
    background: url("images/am_photography_bg_blured.jpg") no-repeat center -25px fixed,
    opacity: .5,
}, 1000, function() {
    // Animation complete.
});

为了实现这一切:

$(document).ready(function() {

$('body').css('display', 'none')
$('body').fadeIn(1500);

$("#background").delay(1500).animate({
    background: url("images/am_photography_bg_blured.jpg") no-repeat center -25px fixed,
    opacity: .5,
}, 1000, function() {
    // Animation complete.
});

$("h1").delay(2000).animate({
    top: -30,
    opacity: 1,
}, 700, function() {
    // Animation complete.
});

$('.link').click(function() {
    event.preventDefault();
    newLocation = this.href;
    $('body').fadeOut(500, newpage);
});
});

function newpage() {
    window.location = newLocation;
}

但它破了。

2 个答案:

答案 0 :(得分:2)

这样的东西?

&#13;
&#13;
  $('#background').addClass('background');

setTimeout(function() {
  $('#background').addClass('background-blured');
}, 2000);
&#13;
  .background {
	background: blue url("images/am_photography_bg.jpg") no-repeat center -25px fixed;
	background-size: 100%;
	height: 100vh;
    }

    .background-blured {
    background: red url("images/am_photography_bg_blured.jpg") no-repeat center -25px fixed;
    background-size: 100%;
	height: 100vh;
    opacity: .5;
    }

#background {
   transition: all 2s ease-in-out;
  }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

	<section>
		<div id="background" class="background"></div>
	</section>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你可以用这个:

$(document).load(function(){
    $("#background").fadeIn();
    setTimeout(change_bg() , 3000); //set time that after which, change_bg() will be executed.
});

function change_bg()
{
    $("#background").fadeOut();
    $("#background-blured").fadeIn();
}