我目前正在开展一个项目,我想知道是否有人可以协助这段JS代码。我试图让DIV在页面加载时移动。所以,比如说时间是早上9点;我想让它移动100px和9:30 am移动120px。
非常感谢。
function slide(element, pixels, duration) {
var start = new Date();
var start_x = parseInt(element.style.left, 10);
function frame() {
var now = new Date(),
elap = now - start,
frac = Math.min(elap / duration, 1),
pos = start_x + (pixels * frac);
element.style.left = (pos || 0) + "px";
if (frac < 1) {
window.requestAnimationFrame(frame);
}
}
window.requestAnimationFrame(frame);
}
var div = $('#slideme');
$('#slideme').load('#slideme', function() {
var elem = document.getElementById("slideme");
slide(elem, -200, 1000);
});
答案 0 :(得分:3)
玩这个JQuery:
// Current datetime
var d = new Date();
// Total minutes since midnight
var totalMinutes = (d.getHours() * 60) + d.getMinutes();
// Number of 30 minute intervals (since midnight)
var numberOf30Mins = Math.floor(totalMinutes / 30);
// Work out how many pixels that should be (20px per interval)
var numberOfPixelsToShift = numberOf30Mins * 20;
$('#slideme').css('left', numberOfPixelsToShift);
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用jquery的动画功能。
$(document).ready(function() {
$( "#block" ).animate({
marginLeft: "+1200px",
}, 3000 );
});