简体   繁体   English

如何重置 a.click 切换 function?

[英]How would I reset a .click toggle function?

I'm making a infographic slider and I have buttons when clicked that reveal text.我正在制作一个信息图 slider,点击显示文本时我有按钮。 When I go to the next slider and then hit the previous button and go back my toggles reveal the text and then bounce back to be invisible.当我 go 到下一个 slider 然后点击上一个按钮和 go 返回时,我的切换显示文本然后弹回不可见。

Here is some of the code... I'm not sure if I should be resetting the css or Javascript div where each button is held?这是一些代码...我不确定是否应该重置每个按钮所在的 css 或 Javascript div? Please tell me how I can fix the toggle issue.请告诉我如何解决切换问题。

 $( "#point1" ).click(function() { $( "#timeline1" ).toggle( "slow", function() { // Animation complete. }); }); $( "#point2" ).click(function() { $( "#timeline2" ).toggle( "slow", function() { // Animation complete. }); }); $( "#point3" ).click(function() { $( "#timeline3" ).toggle( "slow", function() { // Animation complete. }); }); $( "#point4" ).click(function() { $( "#timeline4" ).toggle( "slow", function() { // Animation complete. }); }); $( "#point5" ).click(function() { $( "#timeline5" ).toggle( "slow", function() { // Animation complete. }); }); / next and previous navigation functions function showNextScreen() { // check if nav is active if (navActive) { // disable nav navActive = false; // targets the current screen currentScreen = "#screen" + screenNum; // sets next screen number screenNum++; // show/hide nav showHideNav(screenNum); // targets the next screen nextScreen = "#screen" + screenNum; // transitions current screen out gsap.fromTo(currentScreen, { x: 0 }, { duration: duration, delay: 0.5, x: -960, ease: "power2.inOut" }); // shows next screen $(nextScreen).show(); // transitions next screen in gsap.fromTo(nextScreen, { x: 960 }, { duration: duration, delay: 0.5, x: 0, ease: "power2.inOut", onComplete: function() { // hide current screen $(currentScreen).hide(); // enable nav navActive = true; } }); // load function to animate on contents of screen window["loadScreen" + screenNum](); // stop voiceover from playing $("#voiceover").trigger("pause"); } } function showPrevScreen() { // check if nav is active if (navActive) { // disable nav navActive = false; // targets the current screen currentScreen = "#screen" + screenNum; // sets next screen number screenNum--; // show/hide nav showHideNav(screenNum); // targets the next screen prevScreen = "#screen" + screenNum; // transitions current screen out gsap.fromTo(currentScreen, { x: 0 }, { duration: duration, delay: 0.5, x: 960, ease: "power4.inOut" }); // shows previous screen $(prevScreen).show(); // transitions next screen in gsap.fromTo(prevScreen, { x: -960 }, { duration: duration, delay: 0.5, x: 0, ease: "power2.inOut", onComplete: function() { // hide current screen $(currentScreen).hide(); // enable nav navActive = true; } }); // load function to animate on contents of screen window["loadScreen" + screenNum](); // stop voiceover from playing $("#voiceover").trigger("pause"); } } // next and previous button clicks $("#next").click(showNextScreen); $("#prev").click(showPrevScreen);
 #point1 { position: absolute; top: 20%; left: 8%; transform: translate(-50%, -50%); width: 45px; cursor: pointer; }
 <img src="img/SVG/point1.svg" alt="fact" id="point1" class="point"/> <img src="img/SVG/point2.svg" alt="fact" id="point2" class="point"/> <img src="img/SVG/point3.svg" alt="fact" id="point3"class="point" /> <img src="img/SVG/point4.svg" alt="fact" id="point4"class="point" /> <img src="img/SVG/point5.svg" alt="fact" id="point5" class="point"/> <div id="prev">BACK</div> <div id="next">NEXT</div>

You need to use jquery's .off() method to remove the previous click event handler before adding .click() to the #points .在将 .click .click()添加到#points之前,您需要使用 jquery 的.off()方法删除先前的单击事件处理程序。 Try this code试试这个代码

$( "#point1" ).off('click').click(function() {
    $( "#timeline1" ).toggle("slow");
});

$( "#point2" ).off('click').click(function() {
    $( "#timeline2" ).toggle("slow");
});

$( "#point3" ).off('click').click(function() {
    $( "#timeline3" ).toggle("slow");
});

$( "#point4" ).off('click').click(function() {
    $( "#timeline4" ).toggle("slow");
});

$( "#point5" ).off('click').click(function() {
    $( "#timeline5" ).toggle("slow");
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM