JS onresize、onscroll

onresize

window.onresize = function() {
    console.log(222);
}

onscroll

<div style="height:200px;width:100px;background:orange;position:absolute;right:0px;top:200px;"></div>
<script type="text/javascript">
window.onscroll = function() {
     console.log(222);
}

var div = document.getElementsByTagName('div')[0];
//获取元素距离初始化距离顶部的便宜里
var t = div.offsetTop

window.onscroll = function() {
    //获取当前窗口的滚动距离
    var sT = document.documentElement.scrollTop;
    //求和
    var newTop = sT + t;
    //设置
    div.style.top = newTop + 'px';
}
</script>