温馨提示:
本文最后更新于
2026-08-21 16:32:35,
某些文章具有时效性,若有错误或已失效,请在下方留言!
前言
子比美化之给你的网站文章列表添加一个子比原生的滑动动画效果
预览效果
部署教程
将以下代码添加到子比根目录下的func.php文件没有请自行创建
/**
* 子比文章列表添加从下往上滑动重复动画
*/
add_action('wp_footer', 'meirl_zibll_post_list_slideup_repeat_animation', 99);
function meirl_zibll_post_list_slideup_repeat_animation()
{
if (is_admin()) {
return;
}
?>
<script>
(function ($) {
'use strict';
var itemSelector = '.posts-row .posts-item.ajax-item, .ajaxpager .posts-item.ajax-item';
var observeClass = 'obs-animate';
var animationClass = 'ani-slideup';
var repeatClass = 'obs-animate-repeat';
var activeClass = 'in-view';
var observedFlag = 'meirlSlideupObserved';
var scanTimer = null;
var observer = null;
function supportsMotion() {
return 'IntersectionObserver' in window &&
!(window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches);
}
function prepareItem(el) {
if (!el || el.nodeType !== 1) return;
el.classList.add(observeClass, animationClass, repeatClass);
}
function observeItem(el) {
if (!observer || el.dataset[observedFlag] === '1') return;
prepareItem(el);
el.dataset[observedFlag] = '1';
observer.observe(el);
}
function scan() {
$(itemSelector).each(function () {
observeItem(this);
});
}
function scheduleScan() {
clearTimeout(scanTimer);
scanTimer = setTimeout(scan, 80);
}
function initObserver() {
if (!supportsMotion()) {
$(itemSelector).each(function () {
this.classList.remove(observeClass, animationClass, repeatClass, activeClass);
this.style.transitionDelay = '';
});
return;
}
observer = new IntersectionObserver(function (entries) {
var visibleIndex = 0;
var visibleCount = entries.filter(function (entry) {
return entry.isIntersecting;
}).length;
entries.forEach(function (entry) {
var el = entry.target;
if (entry.isIntersecting) {
var delay = visibleIndex * 0.11;
if (visibleCount > 8) {
delay = delay / (visibleCount / 8);
}
el.style.transitionDelay = delay + 's';
el.classList.add(activeClass);
visibleIndex++;
} else {
el.classList.remove(activeClass);
el.style.transitionDelay = '';
}
});
}, {
root: null,
rootMargin: '100px 0px 0px 0px',
threshold: 0.01
});
scan();
}
$(initObserver);
$(document).on('post_ajax.ed auto_fun zib_ajax.success', scheduleScan);
if ('MutationObserver' in window) {
var mutationObserver = new MutationObserver(scheduleScan);
$(function () {
if (document.body) {
mutationObserver.observe(document.body, {
childList: true,
subtree: true
});
}
});
}
})(jQuery);
</script>
<?php
}
THE END


















请登录后查看评论内容