Why JS doesn't want to scroll?

javascript

41 просмотра

2 ответа

here is my code:

function trailer() {
    window.scroll(0,1750);
}

and the html

<li> <a href="#" onclick="trailer()">Trailer GTA 4</a></li>

..When I click on the link, it scrolls to (0,0) instead of scrolling to (0,1750) why ???

Автор: johnathan Mhehdi Источник Размещён: 08.11.2019 11:23

Ответы (2)


1 плюс

Решение

You have two possible solutions, you can link the ID of the element in a a tag, as such,

HTML Example

<a href="#myDiv">Jump to DIV</a>

<div id="myDiv">DIV!</div>

Example

JSFiddle

You need to prevent the default behavior of the link by using return false or event.preventDefault();.

The JavaScript alternative is to use scrollTo, though as @mplungjan has pointed out the difference between scroll and scrollTo is no difference.

Автор: Script47 Размещён: 20.08.2016 02:08

-1 плюса

use window.scrollTo(0,1750)

see: http://www.w3schools.com/jsref/met_win_scrollto.asp for more details how to use scrollTo

Автор: Valor Naram Размещён: 20.08.2016 01:59
Вопросы из категории :
32x32