简体   繁体   中英

Javascript add and remove eventlistner

I have a set of circles (.dot), and when we click one of them it animates another element.

I am trying to set it up so that when one of these circles is clicked, then the mouseover, mouseout and click functions would be inactive for this one item.

I need to use addEventListener and then removeEventListener or something similar, but I am having trouble implementing where this should be setup in the code.

This code pen shows what I mean: https://codepen.io/celli/pen/MMwpjx

For this example I removed my addEventListner on the click function, since it was not working for me, and I am implementing it incorrectly.

// mouseover and mouseout functions
$('.dotWrapper').mouseover(function(event) {
 TweenMax.to($(this).children('.dot'), .25,{scale:3, ease: Circ.easeOut, 
transformOrigin:"50% 50%"});
        });

$('.dotWrapper').mouseout(function(event) {
 TweenMax.to($(this).children('.dot'), .75,{scale:1, ease: Circ.easeOut});
        });

// click function animates
$('.dotWrapper').click(function() {
  TweenMax.to($(this).children('.dot'), .25,{scale:2, ease: Circ.easeOut});
   $('.dot').removeClass('selected');
    $(this).children('.dot').addClass('selected');

  // clear existing lines and dates
   TweenMax.to('.vertLine', .5, {alpha: 0, scaleY:1, ease:Expo.easeOut});
  TweenMax.to('.timelineDate', .5, {alpha: 0, y:-20, ease:Expo.easeOut});

  // animate the date and line
var dateLine = new TimelineMax({ });
  dateLine.to($(this).children('.vertLine'), 1.2, {scaleY:.50,autoAlpha: 1,ease:Expo.easeOut})
          .to($(this).children('.timelineDate'), 1, {autoAlpha: 1,y:20,ease:Expo.easeOut}, '-=1');
        });

请查看是否可以使用.one()而不是.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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