The editors' meeting has been canceled for technical reasons.

MediaWiki:Common.js:修订间差异

来自NeuroWiki
跳转到导航 跳转到搜索
无编辑摘要
无编辑摘要
第8行: 第8行:
     const $container = $(this);
     const $container = $(this);
     const $div = $container.find('.bouncing-div');
     const $div = $container.find('.bouncing-div');
    // 检查是否启用变色功能
    const changeColor = $container.data('changecolor') === 'yes';


     // 初始化位置和速度
     // 初始化位置和速度
第45行: 第48行:
       }
       }


       // 如果发生碰撞,随机变色
       // 如果发生碰撞并且启用变色
       if (collision) {
       if (collision && changeColor) {
         $div.css('background-color', getRandomColor());
         $div.css('background-color', getRandomColor());
       }
       }

2025年1月24日 (五) 01:27的版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
// 检查当前页面的标题或 URL 是否符合条件
$(function () {
  // 如果页面上没有 .bouncing-container,则无需执行
  if (!$('.bouncing-container').length) return;

  $('.bouncing-container').each(function () {
    const $container = $(this);
    const $div = $container.find('.bouncing-div');

    // 检查是否启用变色功能
    const changeColor = $container.data('changecolor') === 'yes';

    // 初始化位置和速度
    let x = Math.random() * ($container.width() - $div.width());
    let y = Math.random() * ($container.height() - $div.height());
    let dx = (Math.random() < 0.5 ? -1 : 1) * (2 + Math.random() * 2); // 随机 x 速度
    let dy = (Math.random() < 0.5 ? -1 : 1) * (2 + Math.random() * 2); // 随机 y 速度

    // 设置初始位置
    $div.css({ left: x, top: y });

    // 随机颜色生成函数
    function getRandomColor() {
      const letters = '0123456789ABCDEF';
      let color = '#';
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    }

    // 移动和碰撞检测函数
    function move() {
      x += dx;
      y += dy;

      // 碰撞检测
      let collision = false;

      if (x <= 0 || x + $div.width() >= $container.width()) {
        dx = -dx; // 反向 x
        collision = true;
      }
      if (y <= 0 || y + $div.height() >= $container.height()) {
        dy = -dy; // 反向 y
        collision = true;
      }

      // 如果发生碰撞并且启用变色
      if (collision && changeColor) {
        $div.css('background-color', getRandomColor());
      }

      // 更新位置
      $div.css({ left: x, top: y });

      // 使用 requestAnimationFrame 来平滑动画
      requestAnimationFrame(move);
    }

    // 开始动画
    move();
  });
});