<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, Word-wrap: break-word! Important; "> < span style> div {height: 100px; width: 100%; background-color: #cccccc; /* margin-top: 20px; */ } div:nth-child(2n) { background-color: #999999; } </style> </head> <body> <div style="position: fixed; z-index:10; top: 0; background-color: green;" > < button onclick = "go ()" > to the specified location < / button > < / div > < div > < / div > < div > < / div > < div > < / div > < div > < / div > < div style = "position: relative;" > <div style="position: relative; Height: 30 px "> < div id =" div1 "> specified location < / div > < / div > < / div > < div > 2 < / div > < div > < / div > < div > < / div > < div > < / div > < div > < / div > <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </body> <script> // OffsetTop // Distance to the top of the parent element, or to the top of the root element (body) if not enabled // offsetParent // 1 Absolute or relative), whose offsetParent attribute takes the value of the root element, usually Body. // 2) When the parent of a page element has a CSS position (Position: Absolute or relative), the offsetParent attribute of the element is its parent element, and all the references of offsetTop and offsetLeft calculations of the element are its parent element. // 3) If neither an element nor its parent element has been CSS positioned (position: absolute or relative), the offsetParent attribute of the element is the closest element in the DOM hierarchy that has been CSS positioned. Function go() {let el = document.getelementById ('div1') let xh = // window.scroll(0,xh-100) window.scroll({top: xh-100, behavior: Function getElementTop(el) {// Get the distance between the current element and the most recent open position (non-static) let parentTop = El.offsetparent let current = el.offsetparent el.offsetParent ParentTop += current. OffsetTop current = current. OffsetParent} Return parentTop} </script> </ HTML >Copy the code