Home > Net >  how to use ternary operator in jquery animate method when it is inside a chain?
how to use ternary operator in jquery animate method when it is inside a chain?

Time:08-21

based on a variable I want to give logic to .animate method in jquery. how can I do that?

my code:

 $('#'   id)
    .delay(shortIntervalTime)
    .animate({opacity: 'show'})
    .delay(longIntervalTime)
    .fadeOut(3000, function () {
      if (flag === true) {
        cycle1(nextId, false);
      } else {
        vdoList = insertIntoArray(vdoListTwo);
        console.log({ vdoList });
        addDisplay(firstAd, true, vdoList);
      }
    });

so if id === 'block3' I want to use .animate({opacity: 'show', bottom: '100px'}) and in other case I want to use .animate({opacity: 'show'})

how can I do this?

CodePudding user response:

You want to conditionally pass the object to animate method. So simply do this as:

.animate(
    id === 'block3'
        ? { opacity: 'show', bottom: '100px' }
        : { opacity: 'show' },
)

You can achive with ternary operator as:

$('#'   id)
    .delay(shortIntervalTime)
    .animate(
        id === 'block3'
            ? { opacity: 'show', bottom: '100px' }
            : { opacity: 'show' },
    )
    .delay(longIntervalTime)
    .fadeOut(3000, function () {
        if (flag === true) {
            cycle1(nextId, false);
        } else {
            vdoList = insertIntoArray(vdoListTwo);
            console.log({ vdoList });
            addDisplay(firstAd, true, vdoList);
        }
    });
  • Related