JavaScript对象中的onscroll div

JavaScript对象中的onscroll div,javascript,Javascript,我想用scroll构建一个div,当你滚动这个div时,它会激活这个函数 我需要在一个对象中构建这个 有没有办法做到这一点 我在这里写了一个我想要的示例源(不起作用) 函数onsc(divName,divChange){ this.play=函数(){ window.onload=函数(){ document.getElementById(divName).onscroll=function(){ 这个。滚动(n) } }; } this.scroll=函数(n){ document.get

我想用scroll构建一个div,当你滚动这个div时,它会激活这个函数

我需要在一个对象中构建这个

有没有办法做到这一点


我在这里写了一个我想要的示例源(不起作用)


函数onsc(divName,divChange){
this.play=函数(){
window.onload=函数(){
document.getElementById(divName).onscroll=function(){
这个。滚动(n)
}
};
}
this.scroll=函数(n){
document.getElementById(divChange).innerHTML=“滚动!”;
}
}
c[1]=新的onsc(“div1”,“div1_i”)。play();
滚动时,此div将更改
txt


您的代码就快到了。我做了一些更改,并为您输入了JSFIDLE

我对你遗漏的内容添加了评论。最重要的是,当您在
onscroll
事件中输入该函数时,该
的上下文会发生变化

JavaScript

function onsc(divName, divChange) {
    // First of all make `this` inherit to below functions
    var self = this;

    this.play = function () {
        document.getElementById(divName).onscroll = function() {
            // Changed this to call the parent and place the correct DIV
            self.scroll(divChange) 
        }
    }

    this.scroll = function (n) {
        document.getElementById(divChange).innerHTML = "you scroll!";
    }
}

c = new onsc("div1", "div1_i").play();
演示


看看我的JSFIDLE:

也许你可以看看我的JSFIDLE,帮我解决问题。我认为您编写的这段代码可以稍加修改,以适应我试图完成的任务,但无法获得帮助。
function onsc(divName, divChange) {
    // First of all make `this` inherit to below functions
    var self = this;

    this.play = function () {
        document.getElementById(divName).onscroll = function() {
            // Changed this to call the parent and place the correct DIV
            self.scroll(divChange) 
        }
    }

    this.scroll = function (n) {
        document.getElementById(divChange).innerHTML = "you scroll!";
    }
}

c = new onsc("div1", "div1_i").play();