LoginSignup
0
0

More than 5 years have passed since last update.

javascriptで、モーダル表示されたときに、背景を固定させる方法

Posted at

タイトルの内容になります。

クリックさたら、bodyタグに下記が指定され、


fiexd{
    position: fixed;
    width: 100%;
    height: 100%;
}

bodyタグにスクロールされた値が、付与され、閉じるときに下記の関数が実行されるとゆう流れになります。


    function stop() {
        let body = document.getElementsByTagName("body")[0];
        let body_style = body.getAttribute("style").replace("top:", "");
        delete_element(body_style);
    }

    function delete_element(num) {
        if (typeof num === "string") {
            re_px = num.replace("px", "");
            re_fix = re_px.replace("-", "");
            type_elm = Number(re_fix);
            window.scrollTo(0, type_elm);
        }
    }


行っていることは大変単純なので、よかったら、利用してみていただけたらと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0