Javascript scrollIntoView()中间对齐? [英] Javascript scrollIntoView() middle alignment?

查看:1298
本文介绍了Javascript scrollIntoView()中间对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript .scrollIntoView(boolean)仅提供两个对齐选项。

Javascript .scrollIntoView(boolean) provide only two alignment option.


  1. 顶部

  2. 底部

如果我想滚动视图怎么办?我想把特定元素放在页面中间的某个位置?

What if I want to scroll the view such that. I want to bring particular element somewhere in middle of the page?

推荐答案

使用 window.scrollTo() 为此。获取要移动的元素的顶部,并减去窗口高度的一半。

Use window.scrollTo() for this. Get the top of the element you want to move to, and subtract one half the window height.

演示: http://jsfiddle.net/ThinkingStiff/MJ69d/

Element.prototype.documentOffsetTop = function () {
    return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop() : 0 );
};

var top = document.getElementById( 'middle' ).documentOffsetTop() - ( window.innerHeight / 2 );
window.scrollTo( 0, top );

这篇关于Javascript scrollIntoView()中间对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆