在公司做材料系統中,需要做一個總是居於右下角的div,但是因為右邊這部分本就是用iframe做的,所以是不好弄的。
一開始,以為用position:fixed,一句css就可以完成,結果在iframe里面這個單頁面倒是可以做到,但是一旦有加上模版頁面,嵌在iframe中,就不行了。
所以不斷搜索啊,問度娘,看到了這個,
<script type="text/javascript"> window.onscroll= window.onresize = window.onload = function (){ var getDiv = document.getElementById('rightBottom'); var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; getDiv.style.left= document.documentElement.clientWidth - getDiv.offsetWidth+'px'; getDiv.style.top = document.documentElement.clientHeight-getDiv.offsetHeight +scrollTop +'px'; } </script>
但是因為我們是嵌套在iframe中,所以我們找的肯定是夫級頁面,所以,代碼應該變成如下
window.parent.onscroll = window.parent.onresize = window.onload =function () { var oFix_box = document.getElementById('select-panel'); var oscrollTop = window.parent.document.documentElement.scrollTop || window.parent.document.body.scrollTop; oFix_box.style.top = oscrollTop + window.parent.document.documentElement.clientHeight - oFix_box.offsetHeight - 224 + 'px'; $("#select-panel").stop(true,true).animate({ "top": top1 }, 700); }
