微件

ScrollText:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
第29行: 第29行:
      
      
     wrappers.forEach(function(wrapper, index) {
     wrappers.forEach(function(wrapper, index) {
      // 跳过已处理的元素
       if (processedElements.has(wrapper)) {
       if (processedElements.has(wrapper)) {
         return;
         return;
第39行: 第38行:
       }
       }
        
        
      // 检查元素是否可见
       var rect = wrapper.getBoundingClientRect();
       var rect = wrapper.getBoundingClientRect();
       if (rect.width === 0 || rect.height === 0) {
       if (rect.width === 0 || rect.height === 0) {
         return; // 元素不可见,跳过
         return;
       }
       }
        
        
第50行: 第48行:
       var animationName = 'scrollText-' + index + '-' + Date.now();
       var animationName = 'scrollText-' + index + '-' + Date.now();
        
        
       setTimeout(function() {
       // 使用双重 RAF 确保布局完成
         var contentHeight = content.scrollHeight;
      requestAnimationFrame(function() {
        var wrapperHeight = wrapper.offsetHeight;
         requestAnimationFrame(function() {
        var scrollDistance = contentHeight - wrapperHeight;
          var contentHeight = content.scrollHeight;
       
          var wrapperHeight = wrapper.offsetHeight;
        // 如果内容未超出容器高度,不需要滚动
          var scrollDistance = contentHeight - wrapperHeight;
        if (scrollDistance <= 0) {
         
          return;
          if (scrollDistance <= 0) {
        }
            return;
       
          }
        var styleId = 'scroll-text-animation-' + index + '-' + Date.now();
         
       
          var styleId = 'scroll-text-animation-' + index + '-' + Date.now();
        var style = document.createElement('style');
         
        style.id = styleId;
          var style = document.createElement('style');
        style.textContent = `
          style.id = styleId;
          @keyframes ${animationName} {
          style.textContent = `
            0% {  
            @keyframes ${animationName} {
               transform: translateY(0);  
              0% { transform: translateY(0); }
               100% { transform: translateY(-${scrollDistance}px); }
             }
             }
            100% {
           `;
              transform: translateY(-${scrollDistance}px);
          document.head.appendChild(style);
            }
         
           }
          var text = content.textContent || content.innerText;
        `;
          var charCount = text.replace(/\s+/g, '').length;
        document.head.appendChild(style);
          var duration = Math.max(speed, charCount / 8);
       
          content.style.animationName = animationName;
        var text = content.textContent || content.innerText;
          content.style.animationDuration = duration + 's';
        var charCount = text.replace(/\s+/g, '').length;
        });
        var duration = Math.max(speed, charCount / 8);
       });
        content.style.animationName = animationName;
        content.style.animationDuration = duration + 's';
       }, 50);
     });
     });
   }
   }
    
    
  // 监听 DOM 变化(模态框打开时触发)
   var observer = new MutationObserver(function(mutations) {
   var observer = new MutationObserver(function(mutations) {
     var shouldInit = false;
     var shouldInit = false;
第101行: 第96行:
      
      
     if (shouldInit) {
     if (shouldInit) {
       setTimeout(initScroll, 100);
       setTimeout(initScroll, 300); // 增加延迟
     }
     }
   });
   });
第112行: 第107行:
   });
   });
    
    
  // 初始加载
   if (document.readyState === 'loading') {
   if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', function() {
     document.addEventListener('DOMContentLoaded', function() {
       setTimeout(initScroll, 100);
       setTimeout(initScroll, 200);
     });
     });
   } else {
   } else {
     setTimeout(initScroll, 100);
     setTimeout(initScroll, 200);
   }
   }
    
    
  // 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, 100);
       setTimeout(initScroll, 300);
     });
     });
   }
   }
})();
})();
</script>
</script>

2025年10月16日 (四) 22:24的版本

<style> .scroll-text {

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

}

.scroll-text .scroll-text-content {

 word-wrap: break-word;
 word-break: break-all;
 position: relative;
 width: 100%;
 text-align: center;
 animation: scrollText linear infinite;

}

.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;
     }
     
     processedElements.add(wrapper);
     
     var speed = parseFloat(content.getAttribute('data-speed')) || 10;
     var animationName = 'scrollText-' + index + '-' + Date.now();
     
     // 使用双重 RAF 确保布局完成
     requestAnimationFrame(function() {
       requestAnimationFrame(function() {
         var contentHeight = content.scrollHeight;
         var wrapperHeight = wrapper.offsetHeight;
         var scrollDistance = contentHeight - wrapperHeight;
         
         if (scrollDistance <= 0) {
           return;
         }
         
         var styleId = 'scroll-text-animation-' + index + '-' + Date.now();
         
         var style = document.createElement('style');
         style.id = styleId;
         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';
       });
     });
   });
 }
 
 var observer = new MutationObserver(function(mutations) {
   var shouldInit = false;
   mutations.forEach(function(mutation) {
     if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
       var target = mutation.target;
       if (target.classList.contains('card-modal') && target.style.display === 'block') {
         shouldInit = true;
       }
     }
     if (mutation.addedNodes.length > 0) {
       shouldInit = true;
     }
   });
   
   if (shouldInit) {
     setTimeout(initScroll, 300); // 增加延迟
   }
 });
 
 observer.observe(document.body, {
   childList: true,
   subtree: true,
   attributes: true,
   attributeFilter: ['style']
 });
 
 if (document.readyState === 'loading') {
   document.addEventListener('DOMContentLoaded', function() {
     setTimeout(initScroll, 200);
   });
 } else {
   setTimeout(initScroll, 200);
 }
 
 if (typeof mw !== 'undefined' && mw.hook) {
   mw.hook('wikipage.content').add(function() {
     setTimeout(initScroll, 300);
   });
 }

})(); </script>