留言板

Setting scrollTop

Jan Tošovský,修改在8 年前。

Setting scrollTop

Liferay Master 帖子: 566 加入日期: 10-7-22 最近的帖子
Dear All,

I am failing to implement the following jQuery snippet in pure AUI
var hash = window.location.hash;
if(hash){ 
	var targetOffset = $(hash).offset().top - 190;
	$('html,body').animate({scrollTop: targetOffset}, 200);
	return false;
}


The purpose of this code is to scroll the top of the page below the floating toolbar to avoid hidding the requested anchor.

I don't insist on that animation.

Currently I have
    AUI().ready(function(A) {
        var hash = window.location.hash;
        if (hash) {
            hash += ",a[name='" + hash.substring(1) + "']";
            var offset = A.one(hash).getXY();
            alert(offset);
            // A.one(hash).setXY(offset[0], offset[1] - 190);
            // A.one('body').setAttribute('scrollTop', offset[1] - 190);
        }
    });


I have no idea how to scroll properly.

Thanks, Jan
Jan Tošovský,修改在8 年前。

RE: Setting scrollTop (答复)

Liferay Master 帖子: 566 加入日期: 10-7-22 最近的帖子
There were two problems in my code.
  • scrollTop is not an attribute
  • scrollTop value is not set on body or html in my case, but on specific container

<aui:script use="aui-base">
    AUI().ready(function(A) {
        var hash = window.location.hash;
        if (hash) {
            var scroller = A.one('#scroller');
            scroller.set('scrollTop', scroller.get('scrollTop') - 80);
        }
    });
</aui:script>