模拟封装fixed

"Hello World, Hello Blog"

Posted by wudimingwo on December 15, 2018
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Element.prototype.fixed = (function() {
        var x = 0,
          y = 0;
        return function(x1, y1) {
          var ele = this;
          ele.style.position = 'absolute';
          window.onscroll = function(e) {
            x = window.pageXOffset;
            y = window.pageYOffset;
            ele.style.left = x1 + x + 'px';
            ele.style.top = y1 + y + 'px';
          }
          ele.style.left = x1 + x + 'px';
          ele.style.top = y1 + y + 'px';
        }
      })();
      div.fixed(500, 400);