<script>
var bounce = {
x:0, y:0, w:200, h:200, // Window position and size
dx:5, dy:5, // Window velocity窗口速度
interval: 100, // Milliseconds between updates毫秒更新
win: null, // The window we will create
timer: null, // Return value of setInterval() // Start the animation
start: function() {
// Start with the window in the center of the screen
/* if(bounce.win && bounce.x != (screen.width - bounce.w)/2){
bounce.win.moveTo(bounce.x,bounce.y);bounce.win.close()}
else {*/
bounce.x = (screen.width - bounce.w)/2;
bounce.y = (screen.height - bounce.h)/2;
//}
// Create the window that we're going to move around
// The javascript: URL is simply a way to display a short document
// The final argument specifies the window size
bounce.win = window.open('javascript:"<h1>BOUNCE!</h1>"', "",
"left=" + bounce.x + ",top=" + bounce.y +
",width=" + bounce.w + ",height=" +bounce.h+
",status=yes");
// Use setInterval() to call the nextFrame() method every interval
// milliseconds. Store the return value so that we can stop the
// animation by passing it to clearInterval().
bounce.timer = setInterval(bounce.nextFrame, bounce.interval);
}, // Stop the animation
stop: function() {
clearInterval(bounce.timer); // Cancel timer
if (!bounce.win.closed) bounce.win.close(); // Close window
},
/*pause:function() {bounce.win.moveTo(bounce.x,bounce.y);clearInterval(bounce.timer);bounce.win.focus();},*/
// Display the next frame of the animation. Invoked by setInterval()
nextFrame: function() {
// If the user closed the window, stop the animation
if (bounce.win.closed) {
clearInterval(bounce.timer);
return;
}
// Bounce if we have reached the right or left edge
if ((bounce.x+bounce.dx > (screen.availWidth - bounce.w)) ||
(bounce.x+bounce.dx < 0)) bounce.dx = -bounce.dx;
// Bounce if we have reached the bottom or top edge
if ((bounce.y+bounce.dy > (screen.availHeight - bounce.h)) ||
(bounce.y+bounce.dy < 0)) bounce.dy = -bounce.dy;
// Update the current position of the window
bounce.x += bounce.dx;
bounce.y += bounce.dy;
// Finally, move the window to the new position
bounce.win.moveTo(bounce.x,bounce.y); // Display current position in window status line
bounce.win.defaultStatus = "(" + bounce.x + "," + bounce.y + ")";
}
}
</script>
<button onclick="bounce.start()">Start</button>
<button onclick="bounce.pause()">Pause</button>
<button onclick="bounce.stop()">Stop</button>
如何让上述代码中JS和HTML彻底分离,让onclick不在html里,试了DOM,操作不能
代码中注释 pause和 start的开始if部分是我加的,做成暂停效果,有兴趣的话请把pause和start按钮合成一体实现开始暂停功能
var bounce = {
x:0, y:0, w:200, h:200, // Window position and size
dx:5, dy:5, // Window velocity窗口速度
interval: 100, // Milliseconds between updates毫秒更新
win: null, // The window we will create
timer: null, // Return value of setInterval() // Start the animation
start: function() {
// Start with the window in the center of the screen
/* if(bounce.win && bounce.x != (screen.width - bounce.w)/2){
bounce.win.moveTo(bounce.x,bounce.y);bounce.win.close()}
else {*/
bounce.x = (screen.width - bounce.w)/2;
bounce.y = (screen.height - bounce.h)/2;
//}
// Create the window that we're going to move around
// The javascript: URL is simply a way to display a short document
// The final argument specifies the window size
bounce.win = window.open('javascript:"<h1>BOUNCE!</h1>"', "",
"left=" + bounce.x + ",top=" + bounce.y +
",width=" + bounce.w + ",height=" +bounce.h+
",status=yes");
// Use setInterval() to call the nextFrame() method every interval
// milliseconds. Store the return value so that we can stop the
// animation by passing it to clearInterval().
bounce.timer = setInterval(bounce.nextFrame, bounce.interval);
}, // Stop the animation
stop: function() {
clearInterval(bounce.timer); // Cancel timer
if (!bounce.win.closed) bounce.win.close(); // Close window
},
/*pause:function() {bounce.win.moveTo(bounce.x,bounce.y);clearInterval(bounce.timer);bounce.win.focus();},*/
// Display the next frame of the animation. Invoked by setInterval()
nextFrame: function() {
// If the user closed the window, stop the animation
if (bounce.win.closed) {
clearInterval(bounce.timer);
return;
}
// Bounce if we have reached the right or left edge
if ((bounce.x+bounce.dx > (screen.availWidth - bounce.w)) ||
(bounce.x+bounce.dx < 0)) bounce.dx = -bounce.dx;
// Bounce if we have reached the bottom or top edge
if ((bounce.y+bounce.dy > (screen.availHeight - bounce.h)) ||
(bounce.y+bounce.dy < 0)) bounce.dy = -bounce.dy;
// Update the current position of the window
bounce.x += bounce.dx;
bounce.y += bounce.dy;
// Finally, move the window to the new position
bounce.win.moveTo(bounce.x,bounce.y); // Display current position in window status line
bounce.win.defaultStatus = "(" + bounce.x + "," + bounce.y + ")";
}
}
</script>
<button onclick="bounce.start()">Start</button>
<button onclick="bounce.pause()">Pause</button>
<button onclick="bounce.stop()">Stop</button>
如何让上述代码中JS和HTML彻底分离,让onclick不在html里,试了DOM,操作不能
代码中注释 pause和 start的开始if部分是我加的,做成暂停效果,有兴趣的话请把pause和start按钮合成一体实现开始暂停功能
解决方案 »
- 请问javascript实例对象问题?
- 时间在firefox里不出来
- 包含客户端js文件???
- 如何实现打开网页跳转到新的网站
- javascript控制的文本框显示,为什么form提交数据提交不了呐?
- 请教,怎样用javascript 读取 数组表单
- jquery datepicker控件求助~~~~~所有分有奉献~~~~~~急~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 求救!关于网页上内容另存为的问题!
- insertRow(),控制tr背景色
- js读取excel文件时,workbooks.open(filePath)出错,只在xp,ie8上无法执行,其他电脑正常
- javascript没有效果
- 求一句读取HTM页面上指定字符的语句
window.onload = function() {
document.getElementById("start").onclick = bounce.start;
document.getElementById("pause").onclick = bounce.pause;
document.getElementById("stop").onclick = bounce.stop;
};
});
<button id="start">start</button>
<button id="pause">pause</button>
<button id="stop">stop</button>
我改成这样window.onload = function(){
var btn = document.getElementsByTagName("button");
btn[0].onclick = function(){bounce.start();}
btn[1].onclick = function(){bounce.pause();}
btn[btn.length - 1].onclick = function(){bounce.stop();}
}可以了可是我想用 文本节点来判断时 就失败了
for(i=0;i<btn.length;i++){
var text = btn[i].firstChild.nodeValue
btn[i].onclick = function(){
if(text = "Start") bounce.start()
else if(text = "Pause") bounce.pause();
else bounce.stop();}
}就出现窗口无法暂停 停止
还有 就是这段代码我改了后 stop按钮后 再start就无法出现新窗口
<script>
var bounce = {
x:0, y:0, w:200, h:200, // Window position and size
dx:5, dy:5, // Window velocity窗口速度
interval: 100, // Milliseconds between updates毫秒更新
win: null, // The window we will create
timer: null, // Return value of setInterval() // Start the animation
start: function() {
// Start with the window in the center of the screen
if(bounce.win && bounce.x != (screen.width - bounce.w)/2){
bounce.win.moveTo(bounce.x,bounce.y);bounce.win.close()}
else {
bounce.x = (screen.width - bounce.w)/2;
bounce.y = (screen.height - bounce.h)/2;
}
// Create the window that we're going to move around
// The javascript: URL is simply a way to display a short document
// The final argument specifies the window size
bounce.win = window.open('javascript:"<h1>BOUNCE!</h1>"', "",
"left=" + bounce.x + ",top=" + bounce.y +
",width=" + bounce.w + ",height=" +bounce.h+
",status=yes");
// Use setInterval() to call the nextFrame() method every interval
// milliseconds. Store the return value so that we can stop the
// animation by passing it to clearInterval().
bounce.timer = setInterval(bounce.nextFrame, bounce.interval);
}, // Stop the animation
stop: function() {
clearInterval(bounce.timer); // Cancel timer
if (!bounce.win.closed) bounce.win.close(); // Close window
},
pause:function() {bounce.win.moveTo(bounce.x,bounce.y);clearInterval(bounce.timer);bounce.win.focus();},
// Display the next frame of the animation. Invoked by setInterval()
nextFrame: function() {
// If the user closed the window, stop the animation
if (bounce.win.closed) {
clearInterval(bounce.timer);
return;
}
// Bounce if we have reached the right or left edge
if ((bounce.x+bounce.dx > (screen.availWidth - bounce.w)) ||
(bounce.x+bounce.dx < 0)) bounce.dx = -bounce.dx;
// Bounce if we have reached the bottom or top edge
if ((bounce.y+bounce.dy > (screen.availHeight - bounce.h)) ||
(bounce.y+bounce.dy < 0)) bounce.dy = -bounce.dy;
// Update the current position of the window
bounce.x += bounce.dx;
bounce.y += bounce.dy;
// Finally, move the window to the new position
bounce.win.moveTo(bounce.x,bounce.y); // Display current position in window status line
bounce.win.defaultStatus = "(" + bounce.x + "," + bounce.y + ")";
}
}
window.onload = function(){
var btn = document.getElementsByTagName("button");
btn[0].onclick = function(){bounce.start();}
btn[1].onclick = function(){bounce.pause();}
btn[btn.length - 1].onclick = function(){bounce.stop();}
/*for(i=0;i<btn.length;i++){
var text = btn[i].firstChild.nodeValue
btn[i].onclick = function(){
if(text = "Start") bounce.start()
else if(text = "Pause") bounce.pause();
else bounce.stop();}
}*/}
</script>
<!--button onclick="bounce.start()">Start</button>
<button onclick="bounce.pause()">Pause</button>
<button onclick="bounce.stop()">Stop</button-->
<button>Start</button>
<button>Pause</button>
<button>Stop</button>
lz有时间的话,推荐你看看jquery,比如《锋利的jquery》之类的,入门比较快,选择器部分看懂了基本就可以用jq了。。