“Spinning menu button HTML/CSS/JS” 可以直接保留不翻译。

huangapple go评论65阅读模式
英文:

Spinning menu button HTML/CSS/JS

问题

以下是您要翻译的代码部分:

我尝试为网站创建一个旋转菜单按钮。我希望它在单击时旋转并更改内容,然后在再次单击时恢复

这是我的代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    //Shops

    const shopButtons = document.querySelector('.shopButtons')

    //Shop animation on click

    shopButtons.addEventListener('click', () => shopButtons.classList.toggle('openMenu'));

<!-- language: lang-css -->

    .shopButtons {
      background-color: blue;
      border: 3px solid black;
      border-radius: 40px;
      position: fixed;
      bottom: 15px;
      left: 15px;
      width: 80px;
      height: 80px;
      box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
      transition: .75s;
      transform: rotate(-359deg);
    }

    .shopButtons.openMenu {
      background-color: rgb(0, 0, 120);
    }

    .shopButtons::after {
      content: 'F6D2'
      color: white;
      font-size: 3em;
      transition: background-color .75s;
      margin: auto;
      border-radius: inherit;
    }

    .openMenu {
      transition: .75s;
      transform: rotate(0deg);
    }

    .openMenu::after {
      background-color: rgb(0, 0, 120);
      color: white;
      content: '
我尝试为网站创建一个旋转菜单按钮。我希望它在单击时旋转并更改内容,然后在再次单击时恢复

这是我的代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    //Shops

    const shopButtons = document.querySelector('.shopButtons')

    //Shop animation on click

    shopButtons.addEventListener('click', () => shopButtons.classList.toggle('openMenu'));

<!-- language: lang-css -->

    .shopButtons {
      background-color: blue;
      border: 3px solid black;
      border-radius: 40px;
      position: fixed;
      bottom: 15px;
      left: 15px;
      width: 80px;
      height: 80px;
      box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
      transition: .75s;
      transform: rotate(-359deg);
    }

    .shopButtons.openMenu {
      background-color: rgb(0, 0, 120);
    }

    .shopButtons::after {
      content: '\1F6D2'
      color: white;
      font-size: 3em;
      transition: background-color .75s;
      margin: auto;
      border-radius: inherit;
    }

    .openMenu {
      transition: .75s;
      transform: rotate(0deg);
    }

    .openMenu::after {
      background-color: rgb(0, 0, 120);
      color: white;
      content: '\00D7'
      font-size: 50px;
    }

<!-- language: lang-html -->

    <html lang="en">

    <head>
      <link rel="stylesheet" href="../css/style.css">
      <title>Website</title>
    </head>

    <body>
      <button class="shopButtons" id="mainShop"></button>

      <script src="../scripts/script.js"></script>
    </body>

    </html>

<!-- end snippet -->
D7'
font-size: 50px; } <!-- language: lang-html --> <html lang="en"> <head> <link rel="stylesheet" href="../css/style.css"> <title>Website</title> </head> <body> <button class="shopButtons" id="mainShop"></button> <script src="../scripts/script.js"></script> </body> </html> <!-- end snippet -->

希望这对您有所帮助,如果您有其他问题,请随时提出。

英文:

I tried making a spinning menu button for a website. I want it to spin and change its content when clicked, then turn back when it's clicked again

Here is my code :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

//Shops

const shopButtons = document.querySelector(&#39;.shopButtons&#39;)

//Shop animation on click

shopButtons.addEventListener(&#39;click&#39;, () =&gt; shopButtons.classList.toggle(&#39;openMenu&#39;));

<!-- language: lang-css -->

.shopButtons {
  background-color: blue;
  border: 3px solid black;
  border-radius: 40px;
  position: fixed;
  bottom: 15px;
  left: 15px;
  width: 80px;
  height: 80px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
  transition: .75s;
  transform: rotate(-359deg);
}

.shopButtons.openMenu {
  background-color: rgb(0, 0, 120);
}

.shopButtons::after {
  content: &#39;F6D2&#39;;
  color: white;
  font-size: 3em;
  transition: background-color .75s;
  margin: auto;
  border-radius: inherit;
}

.openMenu {
  transition: .75s;
  transform: rotate(0deg);
}

.openMenu::after {
  background-color: rgb(0, 0, 120);
  color: white;
  content: &#39;
.shopButtons {
background-color: blue;
border: 3px solid black;
border-radius: 40px;
position: fixed;
bottom: 15px;
left: 15px;
width: 80px;
height: 80px;
box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
transition: .75s;
transform: rotate(-359deg);
}
.shopButtons.openMenu {
background-color: rgb(0, 0, 120);
}
.shopButtons::after {
content: &#39;\1F6D2&#39;;
color: white;
font-size: 3em;
transition: background-color .75s;
margin: auto;
border-radius: inherit;
}
.openMenu {
transition: .75s;
transform: rotate(0deg);
}
.openMenu::after {
background-color: rgb(0, 0, 120);
color: white;
content: &#39;\00D7&#39;;
font-size: 50px;
}
D7&#39;; font-size: 50px; }

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;../css/style.css&quot;&gt;
  &lt;title&gt;Website&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;button class=&quot;shopButtons&quot; id=&quot;mainShop&quot;&gt;&lt;/button&gt;

  &lt;script src=&quot;../scripts/script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

I think it works pretty well (not sure if the code is optimized though), but I am struggling to make it so the shopping cart icon only appears when the button isn't rotating.

As of right now it works when the menu is opened, but on exit, the shopping cart appears before the rotation animation is over

Is there a simple way to do this ?

I have tried delaying the rotation animation, but this only lead to a very slow animation. I also tried delaying the image change using JavaScript, but it wasn't functional.

答案1

得分: 0

发生的情况是,您旋转整个按钮,而不是图标。因此购物车将始终旋转。

我在这里所做的是从.shopButtons按钮中移除变换,然后仅为.openMenu按钮添加了360度的旋转。

也许这对您来说是一个简单的解决方案。

//Shops

const shopButtons = document.querySelector('.shopButtons')

//Shop animation on click

shopButtons.addEventListener('click', () => shopButtons.classList.toggle('openMenu'));
.shopButtons {
  background-color: blue;
  border: 3px solid black;
  border-radius: 40px;
  position: fixed;
  bottom: 15px;
  left: 15px;
  width: 80px;
  height: 80px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}

.shopButtons.openMenu {
  background-color: rgb(0, 0, 120);
}

.shopButtons::after {
  content: 'F6D2';
  color: white;
  font-size: 3em;
  transition: background-color .75s;
  margin: auto;
  border-radius: inherit;
}

.openMenu {
  transition: .75s;
  transform: rotate(360deg);
}

.openMenu::after {
  background-color: rgb(0, 0, 120);
  color: white;
  content: '
.shopButtons {
  background-color: blue;
  border: 3px solid black;
  border-radius: 40px;
  position: fixed;
  bottom: 15px;
  left: 15px;
  width: 80px;
  height: 80px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}

.shopButtons.openMenu {
  background-color: rgb(0, 0, 120);
}

.shopButtons::after {
  content: '\1F6D2';
  color: white;
  font-size: 3em;
  transition: background-color .75s;
  margin: auto;
  border-radius: inherit;
}

.openMenu {
  transition: .75s;
  transform: rotate(360deg);
}

.openMenu::after {
  background-color: rgb(0, 0, 120);
  color: white;
  content: '\00D7';
  font-size: 50px;
}
D7'
;
font-size: 50px; }
<html lang="en">

<head>
  <link rel="stylesheet" href="../css/style.css">
  <title>Website</title>
</head>

<body>
  <button class="shopButtons" id="mainShop"></button>

  <script src="../scripts/script.js"></script>
</body>

</html>
英文:

What is happening is that you are rotating the entire button, rather than the icons. So the shopping cart will always spin.

What I did here is removed the transformation from the .shopButtons button. Then added a 360deg rotation to just the .openMenu button.

Maybe this is an easy solution for you.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

//Shops

const shopButtons = document.querySelector(&#39;.shopButtons&#39;)

//Shop animation on click

shopButtons.addEventListener(&#39;click&#39;, () =&gt; shopButtons.classList.toggle(&#39;openMenu&#39;));

<!-- language: lang-css -->

.shopButtons {
  background-color: blue;
  border: 3px solid black;
  border-radius: 40px;
  position: fixed;
  bottom: 15px;
  left: 15px;
  width: 80px;
  height: 80px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}

.shopButtons.openMenu {
  background-color: rgb(0, 0, 120);
}

.shopButtons::after {
  content: &#39;F6D2&#39;;
  color: white;
  font-size: 3em;
  transition: background-color .75s;
  margin: auto;
  border-radius: inherit;
}

.openMenu {
  transition: .75s;
  transform: rotate(360deg);
}

.openMenu::after {
  background-color: rgb(0, 0, 120);
  color: white;
  content: &#39;
.shopButtons {
background-color: blue;
border: 3px solid black;
border-radius: 40px;
position: fixed;
bottom: 15px;
left: 15px;
width: 80px;
height: 80px;
box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}
.shopButtons.openMenu {
background-color: rgb(0, 0, 120);
}
.shopButtons::after {
content: &#39;\1F6D2&#39;;
color: white;
font-size: 3em;
transition: background-color .75s;
margin: auto;
border-radius: inherit;
}
.openMenu {
transition: .75s;
transform: rotate(360deg);
}
.openMenu::after {
background-color: rgb(0, 0, 120);
color: white;
content: &#39;\00D7&#39;;
font-size: 50px;
}
D7&#39;; font-size: 50px; }

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;../css/style.css&quot;&gt;
  &lt;title&gt;Website&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;button class=&quot;shopButtons&quot; id=&quot;mainShop&quot;&gt;&lt;/button&gt;

  &lt;script src=&quot;../scripts/script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 04:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定