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'); })
第一次接触这种写法想学习学习...处处都是坑....- -。希望大牛能够指点下我这个小白....该怎么样修改才行啊...
1 djkloop OP 第一次刚加载的时候可以执行..但是只要改变窗口就会报错....这是什么原因呢?我传了 id 进去 margintop 应该有值啊.... |
2 djkloop OP 好吧解决了...原因是 setIntreval 也是 window 的下的方法...粗心害死人... setInterval(function () { that.getHeight(id); }, 这样就可以了 |
3 itkdqwzero 2016-11-09 07:42:44 +08:00 via Android 不懂 ,用什么框架吗? nodejs ? 兼容性测试通过吗? 为什么不用 jQuery ? |
![]() | 4 yuuko 2016-11-09 08:49:11 +08:00 via Android 请看 js 事件,把 id 放第二个参数,第一个参数是事件对象 |
5 djkloop OP @itkdqwzero 就是新手想学习学习这种写法,前面没写过这种。 |