我正在尝试做一些 OO JavaScript 来简单地用淡入淡出转换交换图片,但是当页面加载时没有任何反应。
我所有的图像都命名为 Joe_#.jpg,其中 # 是一个数字。
这是我的 imagesJS.js 文件:
//GLOBALS
var delay = 5000;
var opacityDelay = 100;
var pathBeginning = "images\joe\Joe_";
var pathEnding = ".jpg";
//IMAGE CLASS
function imageObj(id, start, end, initDelay) {
this.obj = document.getElementById(id);
this.cur = start;
this.start = start;
this.end = end;
this.initDelay = initDelay;
this.opacity = 1;
this.fadeIn = function() {
if(this.opacity < 1) {
this.opacity = this.opacity + 0.1;
obj.style.opacity = this.opacity;
setTimeout(this.fadeIn(), opacityDelay);
}
}
this.nextImage = function() {
if(this.cur == this.end) {
this.cur = this.start;
}
else {
this.cur++;
}
obj.src = pathBeginning + this.cur.toString() + pathEnding;
this.fadeIn();
}
this.fadeOut = function() {
if(this.opacity > 0.5) {
this.opacity = this.opacity - 0.1;
obj.style.opacity = this.opacity;
setTimeout(this.fadeOut(), opacityDelay);
}
else {
this.nextImage();
}
}
this.process = function() {
setInterval(this.fadeOut(), delay + Math.floor(Math.random()*delay));
}
}
imageObj.prototype.startProcess = function() {
setTimeout(this.process(), this.initDelay);
}
//IMAGE OBJECTS
var img1 = imageObj('img1', 1, 5, 5000);
img1.startProcess();
以下是我如何在 HTML 页面上包含所有内容:
<head>
<!-- Links/Scripts -->
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="myJavascript.js" type="text/javascript"></script>
<script src="imagesJS.js" type="text/javascript"></script>
这是 myJavascript.js 文件(可能与它有关):
// GLOBAL VARIABLES
var body = $('.body');
//***********//
// On Load //
//***********//
$(document).ready(function(){
//MenuItem Color Change
$('.menuItem').mouseover(function(){
$(this).css({"color":"#6699ff"})
}).mouseout(function(){
$(this).css({"color":"black"})
});
});
我应该将对象放在 onLoad 函数中吗?我似乎找不到我的问题。提前致谢!
//############################################### ############################ 更新1
这是更正下面 net.uk.sweet 的建议和此链接中的一些范围问题后的代码Using setTimeout() within a JavaScript class function
更新代码 -
//GLOBALS
var delay = 5000;
var opacityDelay = 100;
var pathBeginning = "images\joe\Joe_";
var pathEnding = ".jpg";
//IMAGE CLASS
function imageObj(id, start, end, initDelay) {
this.obj = document.getElementById(id);
this.cur = start;
this.start = start;
this.end = end;
this.initDelay = initDelay;
this.opacity = 1;
this.fadeIn = function() {
if(this.opacity < 1) {
this.opacity = this.opacity + 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(fadeIn, opacityDelay);
}
}
this.nextImage = function() {
if(this.cur == this.end) {
this.cur = this.start;
}
else {
this.cur++;
}
this.obj.src = pathBeginning + this.cur.toString() + pathEnding;
this.fadeIn();
}
this.fadeOut = function() {
if(this.opacity > 0.5) {
this.opacity = this.opacity - 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(fadeOut, opacityDelay);
}
else {
this.nextImage();
}
}
this.process = function() {
var self = this;
self.fadeOut();
setInterval(function() {self.fadeOut();}, delay+Math.floor(Math.random()*delay));
}
}
imageObj.prototype.startProcess = function() {
var self = this;
setTimeout(function() {self.process();}, this.initDelay);
}
//IMAGE OBJECTS
var img1 = new imageObj('img1', 1, 5, 5000);
img1.startProcess();
不幸的是,它仍然不起作用....有什么想法吗?
//############################################### ############################### 解决方案更新
我必须在我的其他 javascript 文件的 onLoad 函数中添加 startProcess 函数的创建和调用。
这是解决方案代码:
//GLOBALS
var delay = 5000;
var opacityDelay = 100;
var pathBeginning = "images\\joe\\Joe_";
var pathEnding = ".jpg";
//IMAGE CLASS
function imageObj(id, start, end, initDelay) {
this.obj = document.getElementById(id);
this.cur = start;
this.start = start;
this.end = end;
this.initDelay = initDelay;
this.opacity = 1;
this.fadeIn = function() {
var self = this;
if(this.opacity < 1) {
this.opacity = this.opacity + 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(function() {self.fadeIn();}, opacityDelay);
}
}
this.nextImage = function() {
if(this.cur == this.end) {
this.cur = this.start;
}
else {
this.cur++;
}
this.obj.src = pathBeginning + this.cur.toString() + pathEnding;
this.fadeIn();
}
this.fadeOut = function() {
var self = this;
if(this.opacity > 0.2) {
this.opacity = this.opacity - 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(function() {self.fadeOut();}, opacityDelay);
}
else {
self.nextImage();
}
}
this.process = function() {
var self = this;
self.fadeOut();
setInterval(function() {self.fadeOut();}, delay + Math.floor(Math.random()*delay));
}
}
imageObj.prototype.startProcess = function() {
var self = this;
setTimeout(function() {self.process();}, this.initDelay);
}
感谢所有帮助 net.uk.sweet !!!学到了很多关于范围和 chrome 开发工具的知识!希望有一天我能帮助别人!