basado en una variable, quiero darle lógica al método .animate en jquery. ¿Cómo puedo hacer eso?
mi código:
$('#' + 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); } }); entonces si id === 'block3' quiero usar .animate({opacity: 'show', bottom: '100px'}) y en otro caso quiero usar .animate({opacity: 'show'})
¿Cómo puedo hacer esto?
Desea pasar condicionalmente el objeto al método de animate . Así que simplemente haz esto como:
.animate( id === 'block3' ? { opacity: 'show', bottom: '100px' } : { opacity: 'show' }, )Puede lograr con el operador ternario como:
$('#' + 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); } });