Norman
Norman

Reputation: 805

JavaScript: Is it possible to scrollspy on an iFrame's parent?

I'm having a parallax effect inside an iFrame. Because I'd like to avoid double scroll bars, the iFrame is automatically resized to it's actual height.

However, the parallax won't work anymore, because the $(window).scrollTop() will always be 0 inside the iFrame.

Is it possible to scrollspy on an iFrame's parent?

Here is a very simplified snippet:
Inside the iFrame is a script which needs to get the .scrollTop() of $('.get-scroll')

<body class="get-scroll">
   <div class="stuff">
      ...
   </div>
   <iframe>
      <script>
         //do some magic
      </script>
   </iframe>
   <div class="other-stuff">
      ...
   </div>
</body>

Upvotes: 1

Views: 220

Answers (1)

Zenoo
Zenoo

Reputation: 12880

You can use $(window.parent.window).scrollTop() to get the iframe's parent scroll.

See this JSFiddle for an example. (I couldn't do it in a Stack Overflow snippet)

Upvotes: 1

Related Questions