Assuming that the elements with ids 'Cup0', 'Cup1', and 'Cup2' are images, you can use the following JavaScript code to slow down their move speed:

// Get the three cup elements
const cup0 = document.getElementById('Cup0');
const cup1 = document.getElementById('Cup1');
const cup2 = document.getElementById('Cup2');

// Set the position of the cups to absolute
cup0.style.position = 'absolute';
cup1.style.position = 'absolute';
cup2.style.position = 'absolute';

// Set the initial position of the cups
cup0.style.left = '0px';
cup0.style.top = '0px';
cup1.style.left = '100px';
cup1.style.top = '0px';
cup2.style.left = '200px';
cup2.style.top = '0px';

// Set the maximum speed of the cups (in pixels per second)
const MAX_SPEED = 10;

// Set the update interval for the cups (in milliseconds)
const UPDATE_INTERVAL = 50;

// Set the current position and speed of each cup
let cup0Pos = 0;
let cup0Speed = 0;
let cup1Pos = 100;
let cup1Speed = 0;
let cup2Pos = 200;
let cup2Speed = 0;

// Start the update loop for the cups
setInterval(() => {
  // Update the speed of each cup
  cup0Speed += Math.random() * 2 - 1;
  cup1Speed += Math.random() * 2 - 1;
  cup2Speed += Math.random() * 2 - 1;
  
  // Limit the speed of each cup
  cup0Speed = Math.max(-MAX_SPEED, Math.min(MAX_SPEED, cup0Speed));
  cup1Speed = Math.max(-MAX_SPEED, Math.min(MAX_SPEED, cup1Speed));
  cup2Speed = Math.max(-MAX_SPEED, Math.min(MAX_SPEED, cup2Speed));
  
  // Update the position of each cup
  cup0Pos += cup0Speed;
  cup1Pos += cup1Speed;
  cup2Pos += cup2Speed;
  
  // Set the new position of each cup
  cup0.style.left = `${cup0Pos}px`;
  cup1.style.left = `${cup1Pos}px`;
  cup2.style.left = `${cup2Pos}px`;
  
  // Override any CSS transitions that may affect the speed of the cups
  cup0.style.transition = 'none';
  cup1.style.transition = 'none';
  cup2.style.transition = 'none';
}, UPDATE_INTERVAL);

This code sets the position of the cups to absolute, sets their initial position, and starts an update loop that updates the speed and position of each cup every 50 milliseconds. The speed of each cup is randomly adjusted every update, and is limited to a maximum speed of 10 pixels per second. The code also overrides any CSS transitions that may affect the speed of the cups.

Slow Down Moving Elements (Cup0, Cup1, Cup2) in JavaScript

原文地址: https://www.cveoy.top/t/topic/mywg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录