Home > Article > Web Front-end > Mobile phone APP finger sliding switching picture special effects with source code download_jquery
This is a very cool mobile phone APP that slides your finger to switch picture effects. For this APP special effect, users can switch pictures by sliding their fingers left and right on mobile phones, and the same effect can be achieved by using the mouse on desktop devices.
Effect demonstration Source code download
How to use
HTML structure
The HTML structure of this mobile APP to switch picture special effects adopts the HTML structure of nested dc6dce4a544fdca2df29d5ac0ea9906b. Each picture card is wrapped in div.demo__card, which contains the picture, description information and some additional layers. .
<div class="demo__card"> <div class="demo__card__top brown"> <div class="demo__card__img"></div> <p class="demo__card__name">Hungry cat</p> </div> <div class="demo__card__btm"> <p class="demo__card__we">Whatever</p> </div> <div class="demo__card__choice m--reject"></div> <div class="demo__card__choice m--like"></div> <div class="demo__card__drag"></div> </div>
m--reject is the layer when moving the picture to the left, m--like is the layer when moving the picture to the right, demo__card__drag is the drag layer.
JavaScript
In the jQuery code, the pullChange() function is used to set the rotation angle and transparency of the left and right sliding layers. The release() function is used to determine whether the user slides his finger left or right, and adds corresponding classes to the DOM elements for these actions.
function pullChange() { animating = true; deg = pullDeltaX / 10; $card.css('transform', 'translateX(' + pullDeltaX + 'px) rotate(' + deg + 'deg)'); var opacity = pullDeltaX / 100; var rejectOpacity = opacity >= 0 ? 0 : Math.abs(opacity); var likeOpacity = opacity <= 0 ? 0 : opacity; $cardReject.css('opacity', rejectOpacity); $cardLike.css('opacity', likeOpacity); } ; function release() { if (pullDeltaX >= decisionVal) { $card.addClass('to-right'); } else if (pullDeltaX <= -decisionVal) { $card.addClass('to-left'); } if (Math.abs(pullDeltaX) >= decisionVal) { $card.addClass('inactive'); setTimeout(function () { $card.addClass('below').removeClass('inactive to-left to-right'); cardsCounter++; if (cardsCounter === numOfCards) { cardsCounter = 0; $('.demo__card').removeClass('below'); } }, 300); } if (Math.abs(pullDeltaX) < decisionVal) { $card.addClass('reset'); } setTimeout(function () { $card.attr('style', '').removeClass('reset').find('.demo__card__choice').attr('style', ''); pullDeltaX = 0; animating = false; }, 300); };
Finally, listen to mousedown and touchstart events, and perform card switching operations on non-.inactive card elements.
How to use
HTML structure
The HTML structure of this mobile APP to switch picture special effects adopts the HTML structure of nested dc6dce4a544fdca2df29d5ac0ea9906b. Each picture card is wrapped in div.demo__card, which contains the picture, description information and some additional layers. .
<div class="demo__card"> <div class="demo__card__top brown"> <div class="demo__card__img"></div> <p class="demo__card__name">Hungry cat</p> </div> <div class="demo__card__btm"> <p class="demo__card__we">Whatever</p> </div> <div class="demo__card__choice m--reject"></div> <div class="demo__card__choice m--like"></div> <div class="demo__card__drag"></div> </div>
m--reject is the layer when moving the picture to the left, m--like is the layer when moving the picture to the right, demo__card__drag is the drag layer.
JavaScript
In the jQuery code, the pullChange() function is used to set the rotation angle and transparency of the left and right sliding layers. The release() function is used to determine whether the user slides his finger left or right, and adds corresponding classes to the DOM elements for these actions.
function pullChange() { animating = true; deg = pullDeltaX / 10; $card.css('transform', 'translateX(' + pullDeltaX + 'px) rotate(' + deg + 'deg)'); var opacity = pullDeltaX / 100; var rejectOpacity = opacity >= 0 ? 0 : Math.abs(opacity); var likeOpacity = opacity <= 0 ? 0 : opacity; $cardReject.css('opacity', rejectOpacity); $cardLike.css('opacity', likeOpacity); } ; function release() { if (pullDeltaX >= decisionVal) { $card.addClass('to-right'); } else if (pullDeltaX <= -decisionVal) { $card.addClass('to-left'); } if (Math.abs(pullDeltaX) >= decisionVal) { $card.addClass('inactive'); setTimeout(function () { $card.addClass('below').removeClass('inactive to-left to-right'); cardsCounter++; if (cardsCounter === numOfCards) { cardsCounter = 0; $('.demo__card').removeClass('below'); } }, 300); } if (Math.abs(pullDeltaX) < decisionVal) { $card.addClass('reset'); } setTimeout(function () { $card.attr('style', '').removeClass('reset').find('.demo__card__choice').attr('style', ''); pullDeltaX = 0; animating = false; }, 300); };
Finally, listen to mousedown and touchstart events, and perform card switching operations on non-.inactive card elements.
$(document).on('mousedown touchstart', '.demo__card:not(.inactive)', function (e) { if (animating) return; $card = $(this); $cardReject = $('.demo__card__choice.m--reject', $card); $cardLike = $('.demo__card__choice.m--like', $card); var startX = e.pageX || e.originalEvent.touches[0].pageX; $(document).on('mousemove touchmove', function (e) { var x = e.pageX || e.originalEvent.touches[0].pageX; pullDeltaX = x - startX; if (!pullDeltaX) return; pullChange(); }); $(document).on('mouseup touchend', function () { $(document).off('mousemove touchmove mouseup touchend'); if (!pullDeltaX) return; release(); }); });