This article mainly introduces the materials for downloading the source code of the special effects of mobile phone APP finger sliding switching images, for more information, see this article. This is a very cool mobile phone APP. In mobile phones, users can slide between the left and right of their fingers to switch between pictures. The same effect can be achieved through the mouse on desktop devices.
Download demo source code
Usage
HTML Structure
This mobile phone APP switches the HTML structure of image effects using nesting
Every picture card is enclosed in p. demo _ card, which contains pictures, descriptions, and some additional layers.
Hungry cat
Whatever
M -- reject is the layer for moving the image to the left, m -- like is the layer for moving the image to the right, and demo _ card _ drag is the drag layer.
JavaScript
In 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 the corresponding class to the DOM element 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 the mousedown and touchstart events, and perform Card Switching for non-. inactive card elements.
Usage
HTML Structure
This mobile phone APP switches the HTML structure of image effects using nesting
Every picture card is enclosed in p. demo _ card, which contains pictures, descriptions, and some additional layers.
Hungry cat
Whatever
M -- reject is the layer for moving the image to the left, m -- like is the layer for moving the image to the right, and demo _ card _ drag is the drag layer.
JavaScript
In 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 the corresponding class to the DOM element 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 the mousedown and touchstart events, and perform Card Switching for 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(); });});