简体   繁体   English

多个元素使用相同的 css 类,如何仅更改其中一个元素的 css 属性

[英]Several elements use the same css class, how can I change the css properties of only one of those elements

I am creating a little game that should be like a 2d version of "guitar hero" (if you don't know what "guitar hero" is don't worry, it was just to give context).我正在创建一个小游戏,它应该像“吉他英雄”的二维版本(如果您不知道“吉他英雄”是什么,请不要担心,这只是为了提供上下文)。 I have a red square creator function called squareCreator that adds each new square created a CSS class of .newMostLeftNote .我有一个名为squareCreator的红色方块创建器函数,它添加每个new方块创建一个 CSS 类.newMostLeftNote Afterward, I want each one of those squares to fall down (like gravity) using the function fallingMostLeftNote .之后,我希望使用功能fallingMostLeftNote将这些方块中的每一个都倒下(就像重力一样)。 The problem is that the margin-top that function adds to the square generated by the squareCreator adds to every single square at the same time (even before the square is created), so a square could be created when the .newMostLeftNote CSS class has a margin-top of 700 and it appears way at the bottom.问题是该函数添加到squareCreator生成的正方形的margin-top squareCreator添加到每个正方形(甚至在创建正方形之前),因此当.newMostLeftNote CSS 类具有margin-top 为 700,它出现在底部。

How can I make it so that every square that falls, but starts falling after they appear?我怎样才能做到让每一个下降的方块,但在它们出现后开始下降?

在此处输入图片说明

Notice that in this image, every margin-top CSS property for every new generated square is exactly the same.请注意,在此图像中,每个new生成的正方形的每个margin-top CSS 属性都完全相同。

 var mostLeftNoteMarginTop = 0; function squareCreator(){ var newNote = document.createElement("div"); newNote.className = "newMostLeftNote"; document.body.appendChild(newNote); } var generationSpeed = setInterval(squareCreator, 300); function fallingMostLeftNote() { mostLeftNoteMarginTop += 2; $(".newMostLeftNote").css({ 'margin-top': mostLeftNoteMarginTop + 'px' }); } proc = setInterval(fallingMostLeftNote, 5);
 .newMostLeftNote { height: 100px; width: 100px; background-color: red; margin-left: 400px; position: absolute; } .mostLeftNote { height: 100px; width: 100px; background-color: red; margin-left: 300px; position: absolute; } .middleNote { height: 100px; width: 100px; background-color: blue; margin-left: 600px; position: absolute; } .mostRightNote { height: 100px; width: 100px; background-color: green; margin-left: 900px; position: absolute; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Score: 0</h1> <div class="middleNote"></div> <div class="mostLeftNote"></div> <div class="mostRightNote"></div> <div class="scoreLineTop"></div> <div class="scoreLineButtom"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="main.js" charset="utf-8"></script> </body>

Update更新

var squareQuantity = [];


function squareCreator(){
  var newNote = document.createElement("div");
  newNote.className = "newMostLeftNote";
  document.body.appendChild(newNote);
  squareQuantity.push(this.newNote);
}

var generationSpeed = setInterval(squareCreator, 300);


function fallingMostLeftNote() {
  mostLeftNoteMarginTop += 2;
  squareQuantity[2].css({
    'margin-top': mostLeftNoteMarginTop + 'px'
  });
}

Instead of using javascript to update your margin-top, you could use CSS animations .您可以使用CSS 动画,而不是使用 javascript 来更新您的边距顶部。 Each new square will animate independently.每个新方块都将独立动画。

Here's an example for your use case:以下是您的用例的示例:

 function addSquare() { var squaresElement = document.getElementById("squares"); var squareElement = document.createElement("div"); squareElement.className = "square"; squaresElement.append(squareElement); }
 #squares { display: flex; } .square { height: 100px; width: 100px; margin-right: 10px; background-color: red; animation-name: fall; animation-duration: 4s; animation-timing-function: linear; } /* The animation code */ @keyframes fall { from {margin-top: 0px;} to {margin-top: 300px;} }
 <button onclick="addSquare()">Add square</button> <div id="squares"></div>

My approach is giving a css variable while creating divs for transform delay.我的方法是在为转换延迟创建 div 的同时给出一个 css 变量。 If you need more complex movements, you can use the same logic for animation instead of transform.如果您需要更复杂的动作,您可以使用相同的动画逻辑而不是变换。

<div class="parent"></div>
.parent {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
}
.lets-try {
  flex: 1;
  background: #000;
  height: 60px;
  margin-right: 5px;
  margin-left: 5px;
  transition: all 0.5s ease-in-out var(--delay);
}

.lets-try.is-falling {
  margin-top: 100px;
}
let parent = document.querySelector(".parent");
let numOfSquares = 12;
for (let i = 0; i < numOfSquares ; i++) {
  let delay = i * 0.2;
  let div = document.createElement('div');
  div.setAttribute('class', 'lets-try');
  div.setAttribute('style', `--delay:${delay}s`);
  parent.appendChild(div);
}
setTimeout(() => {
  let items = document.querySelectorAll(".lets-try");
  [...items].forEach(item => {
    item.classList.add("is-falling")
  })
}, 1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改与Backbone集合中的模型相关联的几个元素的css属性? - How to change css properties of several elements associated with models in a Backbone collection? 如何将事件侦听器添加到具有相同 class 的多个元素,然后仅为特定的其他元素更改 CSS - How to add event listeners to multiple elements with the same class and then only change CSS for specific other element 如何检查X个具有相同类但不同ID的元素是否具有一个特定的CSS属性相同? - How can i check if X number of elements with same class but different id have one specific css property identical? 如何使用JavaScript或PHP在运行时更改HTML元素的CSS属性? - How I can change the CSS properties of HTML elements in run time using JavaScript or PHP? jQuery / CSS-将鼠标悬停在具有相同href的元素上更改CSS类 - jQuery/CSS - Change css class on hover on elements with same href 我如何使用jQuery更改与类条件匹配的直接img兄弟元素的CSS - How do I use jQuery to change the CSS of direct img sibling elements matching a class conditional 如何更改动态创建的元素的 css? - How can I change css for elements created dynamically? 如何更改屏幕上未显示的元素的CSS可见性样式? - How can I change the CSS visibility style for elements that are not on the screen? 快速更改元素的CSS类 - Change elements' CSS class on the fly 几个CSS元素的条件 - Conditionals for several CSS elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM