英文:
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('.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: '.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;
}
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 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('.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);
}
.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;
}
<!-- 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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论