最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I need to horizontally scroll to a div id using React.JS ? - Stack Overflow

programmeradmin3浏览0评论
scrollFunc()
{    
     document.getElementById("myID").scrollIntoView({"block":"center"})    
}

I tried scrollIntoView({"block":"center"}) works perfect in chrome but has issues on Internet Explorer !

scrollFunc()
{    
     document.getElementById("myID").scrollIntoView({"block":"center"})    
}

I tried scrollIntoView({"block":"center"}) works perfect in chrome but has issues on Internet Explorer !

Share Improve this question asked Nov 10, 2017 at 7:59 shubham pandeyshubham pandey 772 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

scrollIntoView has partial support in IE/Edge.

scrollIntoView({"block":"center"}) is not supported by IE/Edge


  • You have the option scrollIntoView(true) which corresponds to

scrollIntoView({block: "start", inline: "nearest"})

  • You have the option scrollIntoView(false) which corresponds to

scrollIntoView({block: "end", inline: "nearest"})


I would remend using other API or approach if you need {"block":"center"}.

Read more about it here.

https://developer.mozilla/en-US/docs/Web/API/Element/scrollIntoView https://caniuse./#search=scroll

Give this a try,

To get the position of any element in page, you can use 'getBoundingClientRect' method like this, and window.scrollTo() to scroll to that particular location.

var htmlElement = document.getElementById('container'); var elementPosition = element.getBoundingClientRect() window.scrollTo(elementPosition.left, 0)

Also refer this link for more learning: https://developer.mozilla/en-US/docs/Web/API/Element/getBoundingClientRect

发布评论

评论列表(0)

  1. 暂无评论