“Toggle and background JavaScript”

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

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">&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>
</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(&#39;btn-close&#39;);
let fo = document.getElementById(&#39;fo&#39;);
let btnk = document.getElementById(&#39;btnk&#39;);
let btnk1 = document.getElementById(&#39;btnk1&#39;);
let btnk2 = document.getElementById(&#39;btnk2&#39;);
let btnk3 = document.getElementById(&#39;btnk3&#39;);
fo.style = &quot;display: block;&quot;
fo.classList.toggle(&quot;fo&quot;);
btn.addEventListener(&#39;click&#39;,function(){
    fo.classList.toggle(&quot;fo&quot;);
});
btnk.addEventListener(&#39;click&#39;,function(){
    fo.classList.toggle(&quot;fo&quot;);
});
btnk1.addEventListener(&#39;click&#39;,function(){
    fo.classList.toggle(&quot;fo&quot;);
});
btnk2.addEventListener(&#39;click&#39;,function(){
    fo.classList.toggle(&quot;fo&quot;);
});
btnk3.addEventListener(&#39;click&#39;,function(){
    fo.classList.toggle(&quot;fo&quot;);
});

<!-- 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 -->

&lt;div id=&quot;fo&quot; class=&quot;fo&quot;&gt;
  &lt;div style=&quot;background-color: #000; transform: translate(50%,30%); border-radius: 30px; height: 70%; width: 50%;&quot;&gt;
    &lt;button id=&quot;btn-close&quot; style=&quot;border: 0; background-color: #000; margin-top: 2%; font-size: 3rem; color: white; float: right;&quot;&gt;&lt;span class=&quot;btn-form-close&quot;&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
&lt;/div&gt;
 &lt;button id=&quot;btnk&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;
 &lt;button id=&quot;btnk1&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;
 &lt;button id=&quot;btnk2&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;
 &lt;button id=&quot;btnk3&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;

<!-- 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
&lt;div id=&quot;fo&quot; class=&quot;fo&quot; data-active=&quot;false&quot;&gt;
  &lt;div style=&quot;background-color: #000; transform: translate(50%,30%); border-radius: 30px; height: 70%; width: 50%;&quot;&gt;
    &lt;span id=&quot;btn-close&quot; style=&quot;border: 0; background-color: #000; margin-top: 2%; font-size: 3rem; color: white; float: right;&quot;&gt;
      &lt;span class=&quot;btn-form-close&quot;&gt;&amp;times;&lt;/span&gt;
    &lt;/span&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;button id=&quot;btnk&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;
&lt;button id=&quot;btnk1&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;
&lt;button id=&quot;btnk2&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;
&lt;button id=&quot;btnk3&quot; class=&quot;but-oferts&quot;&gt;Otworz&lt;/button&gt;

CSS

  • target the fo div with the id rather than the class
  • 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=&#39;false&#39;])
#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=&#39;false&#39;] {
  transform: translateX(-850px);
} 
#fo[data-active=&#39;true&#39;] {
  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(&#39;btn-close&#39;);
let fo = document.getElementById(&#39;fo&#39;);
let btnk = document.getElementById(&#39;btnk&#39;);
let btnk1 = document.getElementById(&#39;btnk1&#39;);
let btnk2 = document.getElementById(&#39;btnk2&#39;);
let btnk3 = document.getElementById(&#39;btnk3&#39;);

function toggleMenu() {
  const active = fo.dataset.active
  fo.dataset.active = active == &#39;true&#39; ? &#39;false&#39; : &#39;true&#39;
}

btn.addEventListener(&#39;click&#39;,function(){
    toggleMenu()
});
btnk.addEventListener(&#39;click&#39;,function(){
    toggleMenu()
});
btnk1.addEventListener(&#39;click&#39;,function(){
    toggleMenu()
});
btnk2.addEventListener(&#39;click&#39;,function(){
    toggleMenu()
});
btnk3.addEventListener(&#39;click&#39;,function(){
    toggleMenu()
});

huangapple
  • 本文由 发表于 2023年3月21日 02:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793876.html
匿名

发表评论

匿名网友

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

确定