admin管理员组

文章数量:1191306

I have this url http://localhost:8080/?london and it needs to load to the id of london in <section id="london">.- which is on the page. I cannot use http://localhost:8080/#london, even though that will work!

To make things more complicated I am using a vue framework and they want the code inside master-origin/src/assets/js/mixins/Anchor.js - therefore I can't install jquery, has to be vanilla js.

I have tried

var el = [];
el = window.location.href.split("/?")[1];
console.log("el: " + el);
el.scrollIntoView();

Which grabs the value after the /?, but I when I tried to scrollIntoView, I get this message in the console

TypeError: el.scrollIntoView is not a function at VM10022 IE work:4

Why? I am following w3 schools guide .asp

I have this url http://localhost:8080/?london and it needs to load to the id of london in <section id="london">.- which is on the page. I cannot use http://localhost:8080/#london, even though that will work!

To make things more complicated I am using a vue framework and they want the code inside master-origin/src/assets/js/mixins/Anchor.js - therefore I can't install jquery, has to be vanilla js.

I have tried

var el = [];
el = window.location.href.split("/?")[1];
console.log("el: " + el);
el.scrollIntoView();

Which grabs the value after the /?, but I when I tried to scrollIntoView, I get this message in the console

TypeError: el.scrollIntoView is not a function at VM10022 IE work:4

Why? I am following w3 schools guide https://www.w3schools.com/jsref/met_element_scrollintoview.asp

Share Improve this question asked Nov 17, 2018 at 14:25 artworkjpmartworkjpm 1,3372 gold badges21 silver badges49 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

You try to call .scrollIntoView() on a string.

Try this: document.getElementById(el).scrollIntoView();

Works great for me

const el = document.getElementById('item');
      el.scrollIntoView({behavior: "smooth"});
  window.scrollTo({
        top: document.getElementById("allCars").offsetTop,
        left: 0,
        behavior: "smooth",
      });

本文标签: javascriptscroll to id element when using quotquot in urlvuejsStack Overflow