推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
djkloop

js 小白问个小问题希望各位大牛指点下迷津

  •  
  •   djkloop · Nov 8, 2016 · 2973 views
    This topic created in 3478 days ago, the information mentioned may be changed or developed.

    index.js
    define(function() {

    function Window() {
    
    }
    
    Window.prototype = {
    
        //window 事件
        windowscreen: function(id) {
            //window.onload = this.getHeight(options);
            var that = this;
            window.onresize = function(id) {
                    that.getHeight(id);
                   //执行 onresize 函数的时候浏览器报错
                   // index.js:32 Uncaught TypeError: Cannot read property 'style' of null
            }
            setInterval(this.getHeight(id), 1000);
        },
        //获取屏幕高度让指定元素垂直居中
        getHeight: function(id) {
            var oDiv = document.getElementById(id),
                screenHeight = document.documentElement.clientHeight;
                oDiv.style.marginTop = (parseInt(screenHeight / 2)) + 'px';
        }
    }
    return {
        Window: Window
    };
    

    });

    这是另外一个 js 文件 main.js-- require.config({ paths:{ "jquery": "jquery.min" } })

    require(['jquery','index'],function($,i) { var getHeight = new i.Window(); // getHeight.getHeight('header'); // getHeight.getHeight('main'); // getHeight.getHeight('bottom'); getHeight.getHeight('header'); getHeight.windowscreen('header'); })

    第一次接触这种写法想学习学习...处处都是坑....- -。希望大牛能够指点下我这个小白....该怎么样修改才行啊...

    6 replies    2016-11-09 12:03:28 +08:00
    djkloop
        1
    djkloop  
    OP
       Nov 8, 2016
    第一次刚加载的时候可以执行..但是只要改变窗口就会报错....这是什么原因呢?我传了 id 进去 margintop 应该有值啊....
    djkloop
        2
    djkloop  
    OP
       Nov 8, 2016
    好吧解决了...原因是 setIntreval 也是 window 的下的方法...粗心害死人...
    setInterval(function () {
    that.getHeight(id);
    },
    这样就可以了
    itkdqwzero
        3
    itkdqwzero  
       Nov 9, 2016 via Android
    不懂 ,用什么框架吗?
    nodejs ?
    兼容性测试通过吗?
    为什么不用 jQuery ?
    yuuko
        4
    yuuko  
       Nov 9, 2016 via Android
    请看 js 事件,把 id 放第二个参数,第一个参数是事件对象
    djkloop
        5
    djkloop  
    OP
       Nov 9, 2016 via iPhone
    @itkdqwzero 就是新手想学习学习这种写法,前面没写过这种。😳
    djkloop
        6
    djkloop  
    OP
       Nov 9, 2016 via iPhone
    @yuuko 嗯嗯,好的,谢谢您。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5417 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 06:37 · PVG 14:37 · LAX 23:37 · JFK 02:37
    ♥ Do have faith in what you're doing.