- Products -


Software


NEWS


Special



- Knowledge -


Category × Tag



- Like -

公開
作成日:2022/5/3
更新日:2022/5/17

自動スクロールで指定された要素が表示されるようにする方法

・URLパラメータで指定された文字列要素を表示するようにスクロールするプログラム例を示す。

実装例



function getParam(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

$(function () {
    var targetText = getParam("link", location.href);

    $("h1,h2,h3").each(
        function () {
            if ($(this).text().indexOf(targetText) !== -1) {
                console.log(this);
                setTimeout(() => {
                    this.scrollIntoView();    
                }, 1000);
            }
        });
});


Category



Tag




関連記事


{{tmp.name}}

{{article.category}} {{article.title}}