gpt4 book ai didi

JavaScript - 在固定间隔后连续减小 div 宽度

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:11 25 4
gpt4 key购买 nike

我是一个 JavaScript 新手,我正在尝试完成以下任务:

  1. 一个行为类似于按钮的 div,其宽度 (BG) 每增加一次点击(想象一个随着每次点击而增加的进度条)一定数量,比如每次点击增加 10%
  2. 父div宽度满后,应该有一个10 秒延迟,之后 div 再次开始以恒定速率收缩。(有点像反向进度条)
  3. 当宽度为减少,用户应该能够点击 div 并开始再次增加它直到达到全宽。

请查看下图以供引用:

enter image description here

我已经完成了第 1 步,我认为一旦我能够实现第 2 步,第 3 步就会自行解决。

请帮助我完成第 2 步(不断减小背景 div 的宽度)。

我的代码如下:

var paddleBtn = document.getElementById('paddleBtn');
var paddleC = document.getElementById('paddleC');
if (paddleC) {
paddleC.style.width = "0px";
}
if (paddleBtn) {
paddleBtn.style.cursor = 'pointer';
paddleBtn.onclick = function() {
if ((parseInt(paddleC.style.width) + 5) < paddleC.parentElement.offsetWidth) {
var widthC = parseInt(paddleC.style.width) + 20;
paddleC.style.width = String(widthC).concat('px');
if (paddleC.offsetWidth > paddleC.parentElement.offsetWidth) {
paddleC.style.width = paddleC.parentElement.offsetWidth;
}
}
if (parseInt(paddleC.style.width) >= paddleC.parentElement.offsetWidth - 5) {
paddleC.style.backgroundColor = "#B3DDE0";
paddleC.style.width = paddleC.parentElement.offsetWidth - 2;
}
};
}
.btn {
display: inline-block;
border: 1px solid Black;
height: 30px;
width: 100px;
}

.cooldown {
z-index: 1;
position: fixed;
height: inherit;
background-color: #DCDCDC;
-webkit-transition: width 0.4s ease-in-out;
-moz-transition: width 0.4s ease-in-out;
-o-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
transition: background-color 0.2s ease;
}

.labelT {
z-index: 2;
position: fixed;
padding: 5px;
text-align: center;
font-size: 18px;
font-family: "Arial", Times, serif;
width: inherit;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
<html>

<head>
<script src="./script.js" async></script>
</head>

<body>
<div class='btn' id='paddleBtn'>
<div class='labelT'>Paddle</div>
<div class='cooldown' id='paddleC'></div>
</div>
</body>

</html>

抱歉,代码太丑了,正如我所说,我仍在努力掌握 JavaScript 基础知识。我不得不修改代码示例以使其在 StackOverflow 上运行。

请帮忙!

谢谢!维克西特

最佳答案

你可以试试这个。您可以根据您的要求更改间隔时间,我现在放 3 秒。谢谢

   

var paddleInterval;
var paddleBtn = document.getElementById('paddleBtn');
var paddleC = document.getElementById('paddleC');
function decreasePaddle(){
var widthC = parseInt(paddleC.style.width) - 20;
paddleC.style.width = String(widthC).concat('px');
paddleC.style.backgroundColor = "#DCDCDC";
if (parseInt(paddleC.style.width) <= 0){
clearInterval(paddleInterval);
}
}
if (paddleC) {
paddleC.style.width = "0px";
}
if (paddleBtn) {
paddleBtn.style.cursor = 'pointer';
paddleBtn.onclick = function() {
if ((parseInt(paddleC.style.width) + 5) < paddleC.parentElement.offsetWidth) {
clearInterval(paddleInterval);
var widthC = parseInt(paddleC.style.width) + 20;
paddleC.style.width = String(widthC).concat('px');
if (paddleC.offsetWidth > paddleC.parentElement.offsetWidth) {
paddleC.style.width = paddleC.parentElement.offsetWidth;
}
}
if (parseInt(paddleC.style.width) >= paddleC.parentElement.offsetWidth - 5) {
paddleC.style.backgroundColor = "#B3DDE0";
paddleC.style.width = paddleC.parentElement.offsetWidth - 2;
paddleInterval = setInterval(decreasePaddle, 3000);
}
};
}
.btn {
display: inline-block;
border: 1px solid Black;
height: 30px;
width: 100px;
}

.cooldown {
z-index: 1;
position: fixed;
height: inherit;
background-color: #DCDCDC;
-webkit-transition: width 0.4s ease-in-out;
-moz-transition: width 0.4s ease-in-out;
-o-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
transition: background-color 0.2s ease;
}

.labelT {
z-index: 2;
position: fixed;
padding: 5px;
text-align: center;
font-size: 18px;
font-family: "Arial", Times, serif;
width: inherit;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
<html>

<head>
<script src="./script.js" async></script>
</head>

<body>
<div class='btn' id='paddleBtn'>
<div class='labelT'>Paddle</div>
<div class='cooldown' id='paddleC'></div>
</div>
</body>

</html>

关于JavaScript - 在固定间隔后连续减小 div 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47769958/

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