为了账号安全,请及时绑定邮箱和手机立即绑定

如何在JS中写CSS?

如何在JS中写CSS?

慕少森 2023-10-04 15:54:13
我想做一个记忆游戏。我想在 JS 中为翻转编写一个 CSS 动画,这样我就可以调用一个函数,因为我想制作一个 onclick 动画而不是悬停动画。如何使用 Javascript 中的 oncklicked 函数制作 CSS 翻转动画?var card = "<div class='flip-card'><div class='flip-card-inner'><div class='flip-card-front'><button id='button'onclick='Flipfront()'style='width:300px;height:300px; marign:50px; background-image:url(Frontpage.jpg);'></button></div><div class='flip-card-back'><button id='button2' onclick='Flipback()'style='width:300px;height:300px; marign:50px; background-image:url(IMG1.jpg);'></button></div></div></div>"for (let i = 0; i < 20; i++) {  document.querySelector("#container").innerHTML += card;}function Flipfront() {   // ?}function Flipback() {   // ?}.flip-card {  background-color: transparent;  width: 300px;  height: 200px;  border: 1px solid #f1f1f1;  perspective: 1000px;  /* Remove this if you don't want the 3D effect */}/* This container is needed to position the front and back side */.flip-card-inner {  position: relative;  width: 100%;  height: 100%;  text-align: center;  transition: transform 0.8s;  transform-style: preserve-3d;}/* Do an horizontal flip when you move the mouse over the flip box container */.flip-card:hover .flip-card-inner {  transform: rotateY(180deg);}/* Position the front and back side */.flip-card-front,.flip-card-back {  position: absolute;  width: 100%;  height: 100%;  -webkit-backface-visibility: hidden;  /* Safari */  backface-visibility: hidden;}/* Style the front side (fallback if image is missing) */.flip-card-front {  background-color: #bbb;  color: black;}/* Style the back side */.flip-card-back {  transform: rotateY(180deg);}<div id="outerbackground">  <img src="background.jpg" class="backgorund" border="1" id="BG"></div><div id="container"></div>
查看完整描述

3 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

您是否尝试过点击时动态更改类?.flip-card-inner单击元素时,您可以使用classlist属性及其方法添加类并删除 '.flip-card-front`

用法是:

elem.classList.add("flip-card-inner");

elem.classList.remove("flip-card-front");


查看完整回答
反对 回复 2023-10-04
?
偶然的你

TA贡献1841条经验 获得超3个赞

为了进一步阐述我的评论::hover您可以使用类来.flipped控制卡片的翻转状态,而不是使用 。

然后,在Flipfront()Flipback()方法中,确保接受将从标记传入的参数,该参数将作为Flipfront(this)或调用Flipback(this)。这将允许您访问触发它的元素。

然后,只需使用Element.closest()访问.flip-card父类,并使用Element.classList.add()Element.classList.remove()切换flipped类:

var card = "<div class='flip-card'><div class='flip-card-inner'><div class='flip-card-front'><button id='button'onclick='Flipfront(this)'style='width:300px;height:300px; marign:50px; background-image:url(Frontpage.jpg);'></button></div><div class='flip-card-back'><button id='button2' onclick='Flipback(this)'style='width:300px;height:300px; marign:50px; background-image:url(IMG1.jpg);'></button></div></div></div>"


for (let i = 0; i < 20; i++) {

  document.querySelector("#container").innerHTML += card;

}


function Flipfront(el) { 

  el.closest('.flip-card').classList.add('flipped');

}


function Flipback(el) { 

  el.closest('.flip-card').classList.remove('flipped');

}

.flip-card {

  background-color: transparent;

  width: 300px;

  height: 200px;

  border: 1px solid #f1f1f1;

  perspective: 1000px;

  /* Remove this if you don't want the 3D effect */

}



/* This container is needed to position the front and back side */


.flip-card-inner {

  position: relative;

  width: 100%;

  height: 100%;

  text-align: center;

  transition: transform 0.8s;

  transform-style: preserve-3d;

}



/* Do an horizontal flip when you move the mouse over the flip box container */


.flip-card.flipped .flip-card-inner {

  transform: rotateY(180deg);

}



/* Position the front and back side */


.flip-card-front,

.flip-card-back {

  position: absolute;

  width: 100%;

  height: 100%;

  -webkit-backface-visibility: hidden;

  /* Safari */

  backface-visibility: hidden;

}



/* Style the front side (fallback if image is missing) */


.flip-card-front {

  background-color: #bbb;

  color: black;

}



/* Style the back side */


.flip-card-back {

  transform: rotateY(180deg);

}

<div id="outerbackground">

  <img src="background.jpg" class="backgorund" border="1" id="BG">

</div>

<div id="container"></div>


查看完整回答
反对 回复 2023-10-04
?
狐的传说

TA贡献1804条经验 获得超3个赞

不要用 JS 写 CSS。相反,只需更改:hover规则以取决于.flip-card单击每个类时您切换的类。


另请注意,您不应该使用onX属性,因为它们已经过时,并且由于违反了关注点分离原则而成为不良实践。相反,请使用不显眼的事件处理程序。内联属性也是如此style。将这些规则移至外部样式表中。这是一个工作示例:


let card = '<div class="flip-card"><div class="flip-card-inner"><div class="flip-card-front"><button id="button"></button></div><div class="flip-card-back"><button id="button2"></button></div></div></div>';

for (let i = 0; i < 20; i++) {

  document.querySelector("#container").innerHTML += card;

}


document.querySelectorAll('.flip-card').forEach(el => {

  el.addEventListener('click', () => el.classList.toggle('flipped'));

});

.flip-card {

  background-color: transparent;

  width: 300px;

  height: 200px;

  border: 1px solid #f1f1f1;

  perspective: 1000px;

}


.flip-card-inner {

  position: relative;

  width: 100%;

  height: 100%;

  text-align: center;

  transition: transform 0.8s;

  transform-style: preserve-3d;

}


/* remove :hover here */

.flip-card.flipped .flip-card-inner {

  transform: rotateY(180deg);

}


.flip-card-front,

.flip-card-back {

  position: absolute;

  width: 100%;

  height: 100%;

  -webkit-backface-visibility: hidden;

  /* Safari */

  backface-visibility: hidden;

}


.flip-card-front {

  background-color: #bbb;

  color: black;

}


.flip-card-back {

  transform: rotateY(180deg);

}


#button {

  width: 300px;

  height: 300px;

  background-image: url(Frontpage.jpg);

}


#button2 {

  width: 300px;

  height: 300px;

  background-image: url(IMG1.jpg);

}

<div id="outerbackground">

  <img src="background.jpg" class="backgorund" border="1" id="BG">

</div>

<div id="container"></div>


查看完整回答
反对 回复 2023-10-04
  • 3 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信