英文:
Toogle and background JavaScript
问题
I'm stuck with a problem while learning JavaScript. I want to make an animation when I turn my div on and when I turn it off by clicking the button but I only have an animation when I turn on my div but I can't make an animation when I turn it off how can I do it?
And I have another problem how can I disable my div not only by clicking "x" but when I click anywhere on the screen without that div.
let btn = document.getElementById('btn-close');
let fo = document.getElementById('fo');
let btnk = document.getElementById('btnk');
let btnk1 = document.getElementById('btnk1');
let btnk2 = document.getElementById('btnk2');
let btnk3 = document.getElementById('btnk3');
fo.style = "display: block;";
fo.classList.toggle("fo");
btn.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk1.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk2.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk3.addEventListener('click',function(){
fo.classList.toggle("fo");
});
.fo{
position: fixed;
margin-left: auto;
margin-right: auto;
width: 100%;
height: 100%;
display: none;
animation: ani 0.5s ease;
}
.btn-form-close {
border: 0;
font-size: 3rem;
position: absolute;
top: -2%;
width: 0;
height: 0;
right: 10%;
line-height: normal;
cursor: pointer;
}
@keyframes ani {
0% {
transform: translateX(-850px);
}
100%{
transform: translateX(0px);
}
}
@keyframes ani2 {
0% {
transform: translateX(0px);
}
100%{
transform: translateX(850px);
}
}
<div id="fo" class="fo">
<div style="background-color: #000; transform: translate(50%,30%); border-radius: 30px; height: 70%; width: 50%;">
<button id="btn-close" style="border: 0; background-color: #000; margin-top: 2%; font-size: 3rem; color: white; float: right;"><span class="btn-form-close">×</span></button>
</div>
<button id="btnk" class="but-oferts">Otworz</button>
<button id="btnk1" class="but-oferts">Otworz</button>
<button id="btnk2" class="but-oferts">Otworz</button>
<button id="btnk3" class="but-oferts">Otworz</button>
</div>
Thanks for help anyone! <3
I try to use event.target but its not working.
英文:
I'm stuck with a problem while learning JavaScript. I want to make an animation when I turn my div on and when I turn it off by clicking the button but I only have an animation when I turn on my div but I can't make an animation when I turn it off how can I do it?
And I have another problem how can I disable my div not only by clicking "x" but when I click anywhere on the screen without that div.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let btn = document.getElementById('btn-close');
let fo = document.getElementById('fo');
let btnk = document.getElementById('btnk');
let btnk1 = document.getElementById('btnk1');
let btnk2 = document.getElementById('btnk2');
let btnk3 = document.getElementById('btnk3');
fo.style = "display: block;"
fo.classList.toggle("fo");
btn.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk1.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk2.addEventListener('click',function(){
fo.classList.toggle("fo");
});
btnk3.addEventListener('click',function(){
fo.classList.toggle("fo");
});
<!-- language: lang-css -->
.fo{
position: fixed;
margin-left: auto;
margin-right: auto;
width: 100%;
height: 100%;
display: none;
animation: ani 0.5s ease;
}
.btn-form-close {
border: 0;
font-size: 3rem;
position: absolute;
top: -2%;
width: 0;
height: 0;
right: 10%;
line-height: normal;
cursor: pointer;
}
@keyframes ani {
0% {
transform: translateX(-850px);
}
100%{
transform: translateX(0px);
}
}
@keyframes ani2 {
0% {
transform: translateX(0px);
}
100%{
transform: translateX(850px);
}
}
<!-- language: lang-html -->
<div id="fo" class="fo">
<div style="background-color: #000; transform: translate(50%,30%); border-radius: 30px; height: 70%; width: 50%;">
<button id="btn-close" style="border: 0; background-color: #000; margin-top: 2%; font-size: 3rem; color: white; float: right;"><span class="btn-form-close">&times;</span></button>
</div>
<button id="btnk" class="but-oferts">Otworz</button>
<button id="btnk1" class="but-oferts">Otworz</button>
<button id="btnk2" class="but-oferts">Otworz</button>
<button id="btnk3" class="but-oferts">Otworz</button>
<!-- end snippet -->
Thanks for help anyone! <3
I try to use event.target but its not working.
答案1
得分: 0
HTML:
- 将数据属性添加到`fo` div,以跟踪其打开或关闭状态
- 将内部按钮更改为span,以去除出现在关闭菜单按钮旁边的屏幕上的线
- 在按钮之前添加关闭的`div`标签,以将其与菜单分开
CSS:
- 通过`id`而不是`class`来定位`fo` div
- 使用transform和translateX更改动画为过渡动画。用于决定它是向左还是向右移动的选择器依赖于数据属性(例如,`#fo[data-active='false']`)
JS:
- 将以下内容组合到名为`toggleMenu()`的函数中,以避免重复
- 使用这种方法不需要切换类,因此删除了该部分
- 将当前打开状态存储在名为active的变量中,然后在toggleMenu()函数中将其设置为相反值(三元运算符)
- 使用此函数处理所有四个按钮
英文:
HTML
- added data attribute to the
fo
div to keep track on if it is opened or closed - changed the inner button to span to remove the line that appears on the screen next to the close menu button
- added closing
div
tag before the buttons to separate them from the menu
<div id="fo" class="fo" data-active="false">
<div style="background-color: #000; transform: translate(50%,30%); border-radius: 30px; height: 70%; width: 50%;">
<span id="btn-close" style="border: 0; background-color: #000; margin-top: 2%; font-size: 3rem; color: white; float: right;">
<span class="btn-form-close">&times;</span>
</span>
</div>
</div>
<button id="btnk" class="but-oferts">Otworz</button>
<button id="btnk1" class="but-oferts">Otworz</button>
<button id="btnk2" class="but-oferts">Otworz</button>
<button id="btnk3" class="but-oferts">Otworz</button>
CSS
- target the
fo
div with theid
rather than theclass
- change the animation to a transition using transform and translateX. The selector used to decide if it is moving left or right (850px or -850px) is dependent on the data attribute (e.g.
#fo[data-active='false']
)
#fo{
z-index: 2;
position: fixed;
margin-left: auto;
margin-right: auto;
width: 100%;
height: 100%;
left: -850px;
display: block;
transition: transform 0.5s ease;
}
#fo[data-active='false'] {
transform: translateX(-850px);
}
#fo[data-active='true'] {
transform: translateX(850px);
}
.btn-form-close {
border: 0;
font-size: 3rem;
position: absolute;
top: -2%;
width: 0;
height: 0;
right: 10%;
line-height: normal;
cursor: pointer;
}
JS
- combine the below into a function called
toggleMenu()
so that you are not repeating yourself - you do not need to toggle the class with this approach so that is removed
- store the current open state in a variable called active, then set it to its opposite with the toggleMenu() function (ternary operator)
- use this function for all four buttons
let btn = document.getElementById('btn-close');
let fo = document.getElementById('fo');
let btnk = document.getElementById('btnk');
let btnk1 = document.getElementById('btnk1');
let btnk2 = document.getElementById('btnk2');
let btnk3 = document.getElementById('btnk3');
function toggleMenu() {
const active = fo.dataset.active
fo.dataset.active = active == 'true' ? 'false' : 'true'
}
btn.addEventListener('click',function(){
toggleMenu()
});
btnk.addEventListener('click',function(){
toggleMenu()
});
btnk1.addEventListener('click',function(){
toggleMenu()
});
btnk2.addEventListener('click',function(){
toggleMenu()
});
btnk3.addEventListener('click',function(){
toggleMenu()
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论