微件

ScrollText:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
 
第1行: 第1行:
<!-- ScrollText.html -->
<style>
<style>
.scroll-text {
.scroll-text {
第7行: 第8行:
}
}


/* 动画由 JS 动态设置 */
.scroll-text .scroll-text-content {
.scroll-text .scroll-text-content {
   word-wrap: break-word;
   word-wrap: break-word;
第13行: 第15行:
   width: 100%;
   width: 100%;
   text-align: center;
   text-align: center;
   animation: scrollText linear infinite;
   will-change: transform;
}
}


第24行: 第26行:
(function() {
(function() {
   var processedElements = new WeakSet();
   var processedElements = new WeakSet();
 
 
   function initScroll() {
   function initScroll() {
     var wrappers = document.querySelectorAll('.scroll-text');
     var wrappers = document.querySelectorAll('.scroll-text');
   
 
     wrappers.forEach(function(wrapper, index) {
     wrappers.forEach(function(wrapper, index) {
       if (processedElements.has(wrapper)) {
      // 已完成动画绑定的元素跳过
        return;
       if (processedElements.has(wrapper)) return;
      }
 
     
       var content = wrapper.querySelector('.scroll-text-content');
       var content = wrapper.querySelector('.scroll-text-content');
       if (!content) {
       if (!content) return;
        return;
 
      }
       // 不可见容器跳过(等待视图显示时再初始化)
        
       var rect = wrapper.getBoundingClientRect();
       var rect = wrapper.getBoundingClientRect();
       if (rect.width === 0 || rect.height === 0) {
       if (rect.width === 0 || rect.height === 0) return;
        return;
 
      }
     
      processedElements.add(wrapper);
     
       var speed = parseFloat(content.getAttribute('data-speed')) || 10;
       var speed = parseFloat(content.getAttribute('data-speed')) || 10;
       var animationName = 'scrollText-' + index + '-' + Date.now();
       var animationName = 'scrollText-' + index + '-' + Date.now();
     
 
      // 使用双重 RAF 确保布局完成
       setTimeout(function() {
       requestAnimationFrame(function() {
         var contentHeight = content.scrollHeight;
         requestAnimationFrame(function() {
        var wrapperHeight = wrapper.offsetHeight;
          var contentHeight = content.scrollHeight;
        var scrollDistance = contentHeight - wrapperHeight;
          var wrapperHeight = wrapper.offsetHeight;
 
          var scrollDistance = contentHeight - wrapperHeight;
        // 内容未溢出,不绑定动画,也不标记为 processed,给后续机会
         
        if (scrollDistance <= 0) return;
          if (scrollDistance <= 0) {
 
            return;
        // 动态注入 keyframes
          }
        var style = document.createElement('style');
         
        style.textContent =
          var styleId = 'scroll-text-animation-' + index + '-' + Date.now();
           '@keyframes ' + animationName + ' {' +
         
            '0%{transform:translateY(0)}' +
          var style = document.createElement('style');
            '100%{transform:translateY(-' + scrollDistance + 'px)}' +
          style.id = styleId;
          '}';
           style.textContent = `
 
            @keyframes ${animationName} {
        document.head.appendChild(style);
              0% { transform: translateY(0); }
 
              100% { transform: translateY(-${scrollDistance}px); }
        // 根据字数/速度估算时长
            }
        var text = content.textContent || content.innerText || '';
          `;
        var charCount = text.replace(/\s+/g, '').length;
          document.head.appendChild(style);
        var duration = Math.max(speed, charCount / 8);
         
 
          var text = content.textContent || content.innerText;
        // 设置动画属性
          var charCount = text.replace(/\s+/g, '').length;
        content.style.animationName = animationName;
          var duration = Math.max(speed, charCount / 8);
        content.style.animationDuration = duration + 's';
          content.style.animationName = animationName;
         content.style.animationTimingFunction = 'linear';
          content.style.animationDuration = duration + 's';
        content.style.animationIterationCount = 'infinite';
         });
        content.style.animationPlayState = 'running';
       });
 
        // 仅在成功绑定动画后标记为已处理
        processedElements.add(wrapper);
       }, 50);
     });
     });
   }
   }
    
 
   // 监听自定义事件(视图显示后主动触发初始化)
  document.addEventListener('scrolltext:init', function () {
    setTimeout(initScroll, 50);
  });
 
  // 监听 DOM 变化(模态和各子视图从隐藏到显示时触发)
   var observer = new MutationObserver(function(mutations) {
   var observer = new MutationObserver(function(mutations) {
     var shouldInit = false;
     var shouldInit = false;
     mutations.forEach(function(mutation) {
     mutations.forEach(function(mutation) {
       if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
       if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
         var target = mutation.target;
         var el = mutation.target;
         if (target.classList.contains('card-modal') && target.style.display === 'block') {
        var visible = el.style && el.style.display && el.style.display !== 'none';
         if (visible && (
          el.classList.contains('card-modal') ||
          el.classList.contains('original-card-view') ||
          el.classList.contains('inspiration-view') ||
          el.classList.contains('god-inspiration-view') ||
          el.classList.contains('subcards-view') ||
          el.classList.contains('nested-subcards-view') ||
          el.classList.contains('inspiration-subcards-view') ||
          el.classList.contains('inspiration-nested-subcards-view')
        )) {
           shouldInit = true;
           shouldInit = true;
         }
         }
       }
       }
       if (mutation.addedNodes.length > 0) {
       if (mutation.addedNodes && mutation.addedNodes.length > 0) {
         shouldInit = true;
         shouldInit = true;
       }
       }
     });
     });
   
 
     if (shouldInit) {
     if (shouldInit) {
       setTimeout(initScroll, 300); // 增加延迟
       setTimeout(initScroll, 100);
     }
     }
   });
   });
 
 
   observer.observe(document.body, {
   observer.observe(document.body, {
     childList: true,
     childList: true,
第106行: 第121行:
     attributeFilter: ['style']
     attributeFilter: ['style']
   });
   });
    
 
   // 初始加载
   if (document.readyState === 'loading') {
   if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', function() {
     document.addEventListener('DOMContentLoaded', function() {
       setTimeout(initScroll, 200);
       setTimeout(initScroll, 100);
     });
     });
   } else {
   } else {
     setTimeout(initScroll, 200);
     setTimeout(initScroll, 100);
   }
   }
    
 
   // MediaWiki 钩子
   if (typeof mw !== 'undefined' && mw.hook) {
   if (typeof mw !== 'undefined' && mw.hook) {
     mw.hook('wikipage.content').add(function() {
     mw.hook('wikipage.content').add(function() {
       setTimeout(initScroll, 300);
       setTimeout(initScroll, 100);
     });
     });
   }
   }
})();
})();
</script>
</script>

2025年10月16日 (四) 22:34的最新版本

<style> .scroll-text {

 position: relative;
 overflow: hidden;
 display: flex;
 align-items: flex-start;

}

/* 动画由 JS 动态设置 */ .scroll-text .scroll-text-content {

 word-wrap: break-word;
 word-break: break-all;
 position: relative;
 width: 100%;
 text-align: center;
 will-change: transform;

}

.scroll-text .scroll-text-content:hover {

 animation-play-state: paused;

} </style>

<script> (function() {

 var processedElements = new WeakSet();
 function initScroll() {
   var wrappers = document.querySelectorAll('.scroll-text');
   wrappers.forEach(function(wrapper, index) {
     // 已完成动画绑定的元素跳过
     if (processedElements.has(wrapper)) return;
     var content = wrapper.querySelector('.scroll-text-content');
     if (!content) return;
     // 不可见容器跳过(等待视图显示时再初始化)
     var rect = wrapper.getBoundingClientRect();
     if (rect.width === 0 || rect.height === 0) return;
     var speed = parseFloat(content.getAttribute('data-speed')) || 10;
     var animationName = 'scrollText-' + index + '-' + Date.now();
     setTimeout(function() {
       var contentHeight = content.scrollHeight;
       var wrapperHeight = wrapper.offsetHeight;
       var scrollDistance = contentHeight - wrapperHeight;
       // 内容未溢出,不绑定动画,也不标记为 processed,给后续机会
       if (scrollDistance <= 0) return;
       // 动态注入 keyframes
       var style = document.createElement('style');
       style.textContent =
         '@keyframes ' + animationName + ' {' +
           '0%{transform:translateY(0)}' +
           '100%{transform:translateY(-' + scrollDistance + 'px)}' +
         '}';
       document.head.appendChild(style);
       // 根据字数/速度估算时长
       var text = content.textContent || content.innerText || ;
       var charCount = text.replace(/\s+/g, ).length;
       var duration = Math.max(speed, charCount / 8);
       // 设置动画属性
       content.style.animationName = animationName;
       content.style.animationDuration = duration + 's';
       content.style.animationTimingFunction = 'linear';
       content.style.animationIterationCount = 'infinite';
       content.style.animationPlayState = 'running';
       // 仅在成功绑定动画后标记为已处理
       processedElements.add(wrapper);
     }, 50);
   });
 }
 // 监听自定义事件(视图显示后主动触发初始化)
 document.addEventListener('scrolltext:init', function () {
   setTimeout(initScroll, 50);
 });
 // 监听 DOM 变化(模态和各子视图从隐藏到显示时触发)
 var observer = new MutationObserver(function(mutations) {
   var shouldInit = false;
   mutations.forEach(function(mutation) {
     if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
       var el = mutation.target;
       var visible = el.style && el.style.display && el.style.display !== 'none';
       if (visible && (
         el.classList.contains('card-modal') ||
         el.classList.contains('original-card-view') ||
         el.classList.contains('inspiration-view') ||
         el.classList.contains('god-inspiration-view') ||
         el.classList.contains('subcards-view') ||
         el.classList.contains('nested-subcards-view') ||
         el.classList.contains('inspiration-subcards-view') ||
         el.classList.contains('inspiration-nested-subcards-view')
       )) {
         shouldInit = true;
       }
     }
     if (mutation.addedNodes && mutation.addedNodes.length > 0) {
       shouldInit = true;
     }
   });
   if (shouldInit) {
     setTimeout(initScroll, 100);
   }
 });
 observer.observe(document.body, {
   childList: true,
   subtree: true,
   attributes: true,
   attributeFilter: ['style']
 });
 // 初始加载
 if (document.readyState === 'loading') {
   document.addEventListener('DOMContentLoaded', function() {
     setTimeout(initScroll, 100);
   });
 } else {
   setTimeout(initScroll, 100);
 }
 // MediaWiki 钩子
 if (typeof mw !== 'undefined' && mw.hook) {
   mw.hook('wikipage.content').add(function() {
     setTimeout(initScroll, 100);
   });
 }

})(); </script>