LoginSignup
0

More than 5 years have passed since last update.

iframe(子ページ内)でリンクをクリックしたときに、親ページを上部に移動する

Last updated at Posted at 2017-02-27

子ページに下記を記述。

<script type="text/javascript">
$(function(){
    $('.hoge a').click(function(){
        $('html,body',parent.document).animate({scrollTop:-9999},0);
    });
});
</script>

当然、別途jQueryも読み込む。

子ページのhogeクラスにあるa要素をクリックすると、0秒で親ページが上部に移動します。

-9999という値は、親ページがこれだけ移動すれば上部になるだろうと雑な感じです。

親ページが10000px以上もある縦長のページなら値を変更する必要があります。

親ページの縦サイズを取得して、移動する値に入れる方が、もっとスマートな感じになると思います。

試してみたけど、移動を slow にすると途中で止まるから、諦めて雑な値を入れた所存です。

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