這是一款效果非常酷炫的行動手機APP滑動手指切換圖片特效。此APP特效在行動手機中使用者可以透過手指的左右滑動來切換圖片,在桌上型裝置中透過滑鼠也可達到相同的效果。
使用方法
HTML結構
這個行動手機APP切換圖片特效的HTML結構採用巢狀
<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是向左移動圖片時的圖層,m--like是向右移動圖片時的圖層,demo__card__drag是拖曳層。
JavaScript
在jQuery程式碼中,pullChange()函數用於設定向左和向右兩個滑動層的旋轉角度和透明度。 release()函數用來判斷使用者是向左或向右滑動手指,並為這些動作在DOM元素上加入對應的class。
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); };
最後監聽mousedown和touchstart事件,並對非.inactive的卡片元素執行卡片切換操作。
使用方法
HTML結構
這個行動手機APP切換圖片特效的HTML結構採用巢狀
<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是向左移動圖片時的圖層,m--like是向右移動圖片時的圖層,demo__card__drag是拖曳層。
JavaScript
在jQuery程式碼中,pullChange()函數用於設定向左和向右兩個滑動層的旋轉角度和透明度。 release()函數用來判斷使用者是向左或向右滑動手指,並為這些動作在DOM元素上加入對應的class。
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); };
最後監聽mousedown和touchstart事件,並對非.inactive的卡片元素執行卡片切換操作。
$(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(); }); });