简体   繁体   English

我想在两个功能之间切换,这段代码只工作一次,但我想让它每次点击都工作

[英]i want to toggle between two functions this code works only one time but i want to make it work each time i click

let callOne = true;
let hamburger = document.getElementById("hamburger");

hamburger.addEventListener("click", (e) => {toggleFct();});

function one() {t1.play();callOne = false;}

function two() {t0.play(); callOne = true;}

function toggleFct() { callOne ? one() : two();}

let t1 = gsap.timeline({ paused: true, reversed: true }); let t1 = gsap.timeline({ paused: true, reversed: true });

t1.to(".div-first", { ease: "power2.in", x: "-200%", duration: 2 }); t1.to(".div-first", { ease: "power2.in", x: "-200%", 持续时间: 2 });

t1.to(".div-second", { ease: "power2.in", x: "-300%", duration: 2 },"-=2"); t1.to(".div-second", { ease: "power2.in", x: "-300%", 持续时间: 2 },"-=2");

t1.to(".div-third", { ease: "power2.in", x: "-400%", duration: 2 }, "-=2"); t1.to(".div-third", { ease: "power2.in", x: "-400%", 持续时间: 2 }, "-=2");

t1.to(header, { ease: "power2.in", x: "-400%", duration: 2 }, "-=2"); t1.to(header, { ease: "power2.in", x: "-400%", duration: 2 }, "-=2");

t1.to(wrap, { ease: "power2.in", x: "-400%", duration: 2 }, "-=2"); t1.to(wrap, { ease: "power2.in", x: "-400%", duration: 2 }, "-=2");

let t0 = gsap.timeline({ paused: true, reversed: true }); let t0 = gsap.timeline({ paused: true, reversed: true });

t0.to(".div-first", { ease: "power2.in", x: "100%", duration: 2 }); t0.to(".div-first", { ease: "power2.in", x: "100%", duration: 2 });

t0.to(".div-second", { ease: "power2.in", x: "200%", duration: 2 }, "-=2"); t0.to(".div-second", { ease: "power2.in", x: "200%", duration: 2 }, "-=2");

t0.to(".div-third", { ease: "power2.in", x: "300%", duration: 2 }, "-=2"); t0.to(".div-third", { ease: "power2.in", x: "300%", duration: 2 }, "-=2");

t0.to(header, { ease: "power2.in", x: "400%", duration: 2 }, "-=2"); t0.to(header, { ease: "power2.in", x: "400%", duration: 2 }, "-=2");

t0.to(wrap, { ease: "power2.in", x: "0%", duration: 2 }, "-=2"); t0.to(wrap, { ease: "power2.in", x: "0%", duration: 2 }, "-=2");

let callOne = true;让 callOne = true; let hamburger = document.getElementById("hamburger");让 hamburger = document.getElementById("hamburger");

hamburger.addEventListener("click", (e) => {toggleFct();}); hamburger.addEventListener("点击", (e) => {toggleFct();});

function one() {t1.play(0);callOne = false;} function one() {t1.play(0);callOne = false;}

function two() {t0.play(0); function 二(){t0.play(0); callOne = true;} callOne = 真;}

function toggleFct() { callOne? function toggleFct() { callOne? one(): two();}一二();}

When I try to do the following:当我尝试执行以下操作时:

let callOne = true;

function one() {console.log("one");callOne = false;}

function two() {console.log("two"); callOne = true;}

function toggleFct() { callOne ? one() : two();}

It works just fine...它工作得很好......

btw you can rewrite顺便说一句,你可以重写

hamburger.addEventListener("click", (e) => {toggleFct();});

as...作为...

hamburger.addEventListener("click", toggleFct)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想要一键根据时间在两个功能之间切换? - I want one button to switch between two functions based on time? 我想一键调用两个函数,并确保它们一个接一个地工作 - i want to call two functions on one click and make sure they will work one after another 我想使按钮每次单击增加10像素,但它循环错误 - i want to make button each time i click increase 10 px but it loop error 每次单击按钮,我都想在两个功能之间切换 - On every button click, I want to switch between two functions 两个单独的函数,但是我希望它们同时被初始化 - Two separate Functions, but I want them to be initialized at the same time 我想做一个Javascript测验,每次随机化问题 - I want to make a Javascript Quiz that randomizes the questions each time 我在一个组件中同时使用两个按钮,当我单击它而不是另一个按钮时,我只希望一个按钮起作用 - i am using two buttons simultaneously in one component, i want only one button to work when i click it not the other button 我想单击一个按钮,然后一次从已经制作好的程序中只执行一行代码。.(javascript) - I want to click a button and execute just one line of code at a time from an already made program.. (javascript) 我想要一个按钮中有两个功能 - I want Two functions in one button 我想将这两个功能合而为一 - I want to put these two functions in one function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM