gpt4 book ai didi

javascript - 有没有办法在我们看到一个div一段时间后制作一个动画?

转载 作者:行者123 更新时间:2023-12-04 07:20:27 26 4
gpt4 key购买 nike

我正在尝试做这样的事情......用户点击名为“调试”的按钮,然后窗口滚动到一个带有“谢谢”文本的div(#div2),然后(2秒后#div2 在屏幕上)我希望窗口滚动到包含其余内容的另一个 div (#div3)。问题是我想用平滑的滚动做所有事情,我自己试了 2 天,但还没有结果:(

代码如下:

HTML:

<div id="div1">
<h2>hi! my name is <b>Alan Aguilar</b></h2>
<h3>and i'm a web develop...</h3>
<p class="pIn">ummm...</p>
<p class="pAp">sorry can u press this button for me, please?</p>
<a href="#div2">debug</a>
</div>

<div id="div2">
<h2>Th4nk5.</h2>
</div>

<div id="div3">
<h2>Thanks.</h2>
</div>

CSS:

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
scroll-behavior: smooth;
}

#div1 h2 {
animation: opIn 2s backwards;
}

#div1 h3 {
animation: opIn 1.2s 1.4s backwards;
}

#div1 {
height: 650px;
display: flex;
justify-content: center;
flex-direction: column;
text-align: left;
margin-left: 300px;
}

#div1 h2, h3 {
font-weight: normal;
font-size: 30px;
margin-bottom: 10px;
}

#div1 h3 {
font-size: 23px;
}

.pIn {
font-size: 17px;
margin-top: 30px;
animation: opIn .5s 2.5s backwards;
}

.pAp {
font-size: 17px;
margin-top: 5px;
animation: opIn .5s 3.8s backwards;
}

#div2 {
display: flex;
justify-content: center;
align-items: center;
height: 700px;
background: linear-gradient(to top, #000000 0%, #ffffff 100%);
}

#div1 a {
width: 50px;
padding: 5px;
margin-top: 10px;
position: relative;
left: 245px;
text-decoration: none;
background-color: rgba(0, 0, 0, .1);
border: 1px solid rgba(0, 0, 0, .5);
border-radius: 2px;
animation: opIn .3s 4.5s backwards;
color: #000000;
}

#div1 a:hover {
background-color: rgba(0, 0, 0, .15);
box-shadow: inset 0px 0px 1px #000000;
}

#div2 h2 {
font-size: 190px;
}

#div3 {
display: flex;
justify-content: center;
align-items: center;
height: 700px;
background-color: #000000;
color: #ffffff;
}

@keyframes opIn {
from {opacity: 0;}
to {opacity: 1;}
}

Javascript:

window.onbeforeunload = function () {
window.scrollTo(0, 0);
}

// --------------------------------------------------------------------------
// THIS CODE IS NOT MINE, I FOUND IT ON THIS PAGE LOOKING FOR THE SAME TOPIC.

$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();

// Store hash
var hash = this.hash;

// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){

// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});

// --------------------------------------------------------------------------

$("a").click(function() {
setTimeout(function() {window.location = "#div3"}, 1500, function() {
})
})

最佳答案

选择 ID 为 div3 的元素,获取顶部偏移量,并使用 jQuery 的 animate 函数制作动画:

window.onbeforeunload = function() {
window.scrollTo(0, 0);
}

// --------------------------------------------------------------------------
// THIS CODE IS NOT MINE, I FOUND IT ON THIS PAGE LOOKING FOR THE SAME TOPIC.

$(document).ready(function() {
// Add smooth scrolling to all links
$("a").on('click', function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();

// Store hash
var hash = this.hash;

// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function() {

// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});

// --------------------------------------------------------------------------

$("a").click(function() {
setTimeout(function() {
$('html, body').animate({
scrollTop: $("#div3").offset().top
}, 800);
}, 1500)
})
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
scroll-behavior: smooth;
}

#div1 h2 {
animation: opIn 2s backwards;
}

#div1 h3 {
animation: opIn 1.2s 1.4s backwards;
}

#div1 {
height: 650px;
display: flex;
justify-content: center;
flex-direction: column;
text-align: left;
margin-left: 300px;
}

#div1 h2,
h3 {
font-weight: normal;
font-size: 30px;
margin-bottom: 10px;
}

#div1 h3 {
font-size: 23px;
}

.pIn {
font-size: 17px;
margin-top: 30px;
animation: opIn .5s 2.5s backwards;
}

.pAp {
font-size: 17px;
margin-top: 5px;
animation: opIn .5s 3.8s backwards;
}

#div2 {
display: flex;
justify-content: center;
align-items: center;
height: 700px;
background: linear-gradient(to top, #000000 0%, #ffffff 100%);
}

#div1 a {
width: 50px;
padding: 5px;
margin-top: 10px;
position: relative;
left: 245px;
text-decoration: none;
background-color: rgba(0, 0, 0, .1);
border: 1px solid rgba(0, 0, 0, .5);
border-radius: 2px;
animation: opIn .3s 4.5s backwards;
color: #000000;
}

#div1 a:hover {
background-color: rgba(0, 0, 0, .15);
box-shadow: inset 0px 0px 1px #000000;
}

#div2 h2 {
font-size: 190px;
}

#div3 {
display: flex;
justify-content: center;
align-items: center;
height: 700px;
background-color: #000000;
color: #ffffff;
}

@keyframes opIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div1">
<h2>hi! my name is <b>Alan Aguilar</b></h2>
<h3>and i'm a web develop...</h3>
<p class="pIn">ummm...</p>
<p class="pAp">sorry can u press this button for me, please?</p>
<a href="#div2">debug</a>
</div>

<div id="div2">
<h2>Th4nk5.</h2>
</div>

<div id="div3">
<h2>Thanks.</h2>
</div>

关于javascript - 有没有办法在我们看到一个div一段时间后制作一个动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68536080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com