3

This question may sound silly but it's breaking my head. I'd like to make a sort of vertical srolling presentation, made up of different slides; the effect should be "similar" to this site: http://www.soleilnoir.net/believein/#/start

I've created an unorderd list containing the different slide:

<div id="main">
  <div id="content">
    <ul id="bg">
      <li id="slide1"> <!-- content --></li>
      <li id="slide2"> <!-- content --></li>
    <!-- and so on -->
    </ul>
  </div>
</div>

I've given a position:fixed on the center of the page to all the slides' content (so far are images), and a different z-index to make slides and images stacked. SOmething like

-----  slide 1
  §§§ image 2
-----  slide 2
  §§§ image 3
----   slide 3

and so on

This of course works only in a sense: the slide, when scrolling, uncovers the following slide, but the previous images are always visible being all removed from the flow due to their fixed positioning.

So I thought at giving the images a position:absolute (and give position:relative to the <li>) but that doesn't make them "sticky" in the center of the page until the new slides arrives and covers it, they of course naturally follow their parent <li> in their movement.

I'm completely at loss on this, and also on how to get the current visibile list in the window. I've looked at all the built in functions provided by jQuery: scrollTop(), offset().top, position().top, I got $(window).height(), the $('#container').height(), <li>s height is fixed, etc but all I get are "absolute" positions of the elements (I mean, it doesn't change with scroll), I cannot figure out how to say when a slide is halfway across the screen so that I can do something (still don't know what, though).

I'm sure I'm missing something obvious here. I've studied the code from the linked site, and while I can understand almost everything of it I still can't figure out how it gets the current slide (besides, it works differently from mine, as it loads the animated gifs dinamically and only does that for each slide. In mine each slide is a container with different elements, animation, and so on).

How do I get if, for example, $('li#slide3') is inside view? How can I solve the stacked images problem? Do I need to set them as "fixed" dynamically, or recalculate at every scroll their position so that a position:absolute would do? For this second, I would still need to get the position of each element compared to a fixed point (I believe $(window).scrollTop(), which so far gives me always 0 while scrolling), but I can't figure out how.

Oh, I'm using the jQuery scroll() method to bind the scrolling, as I also have a navigation list which let you jump to the desired slide, and it accounts for this too.

I hope I made myself clear on this: images must always stay at the center, while scrolling the slides need to cover them up and while the image is being covered the following needs to appear and take its place, so I need to understand how to get the position of the current <li> element, say when it's half-way across the window, and set the previous image accordingly.

Damien Pirsy
  • 24,544
  • 6
  • 65
  • 77

2 Answers2

5

That type of website is using Parallax Effects to move the background images.

Consider using a jQuery Parallax plugin, such as jparallax that can make things a lot easier for.

But for a whole webpage, you might want to turn to jQuery Parallax Plugin that has a demo page based on the example you provided. That demo page is also a tutorial too!

There's also a lot of websites that can give you further ideas and by looking inside the webpage, you can find out what type of parallax plugin they are using.

Edit: An excellent tool to use is built right into Firefox browser so you can see any webpage in a layered 3D structure. That will help in getting a handle on how that website is interacting with layered background. More about Firefox 3D View HERE.

Edit 2: This might be useful: How to tell if a DOM element is visible in the current viewport?

Also, for the website example you provided, the method they are using to center the image in the viewport once it's scrolled into view can be seen in their style.css file. Of course they have jQuery modifying these elements, but the style does make sense:

#main ul.bg li img.gif { position: absolute; z-index: 1; width: 996px; height: 800px; left: 50%; right: 50%; top: 50%; bottom: 50%; margin: -400px -498px; }




Edit 3: Here is a jQuery Parallax Plugin that does the same thing as your linked example, and has a very power API at that. This particular plugin has per layer positioning with custom offsets.

This is a website using that plugin in which things line up in the center of the screen.
This other website shows this plugin has no issues when selecting any layer to interact with.

That plugin is named Stellar.js and the gitHub page is HERE. If you visit there home page, it scrolls horizontally... notice the word EASY in stars which is a parallax effect that lines up to the browsers viewport edge. As you scroll, other stars will auto-scroll when in the general area to show other keywords.

Community
  • 1
  • 1
arttronics
  • 9,907
  • 2
  • 24
  • 59
  • I know the parallax plugin and used it in some projects, and that site is not using it - although the effect it achieves can be seen as a parallax-style one. Pretty sure about that, I see it calculates the distance of the pictures each time; I just don't know how it gets the current slide. Anyway, the site you linked to is nice (I alreadt saw the nikeworld one), it could be worth a try althoug is not what I needed – Damien Pirsy Jun 10 '12 at 09:14
  • God I saw it know the site in the list you linked too... a +1 deserved, it doens't answer my question but it could be a nice alternative – Damien Pirsy Jun 10 '12 at 09:23
  • As far as I can see, the example link you provided is just a custom parallax engine being used... a lot of trig math functions are evident in calculating a lot of layer movement going on. – arttronics Jun 10 '12 at 09:25
  • 1
    The benefit of using a parallax plugin (there are many types out there) already has the cross-browser bugs ironed out. Just check out the many versions API to see if it can do what you need it to. – arttronics Jun 10 '12 at 09:28
  • Hi, sorry for the delay. I ended up using some functions taken from teh examples your provided, althoug I unfortunately have a very tight deadline and cannot study all that goodness. Anyway, you gave me great pointers, thanks! – Damien Pirsy Jun 11 '12 at 11:37
-1

I look at HTML evolution, div#container have a big height (number of li * li height), li have display:none/block, overflow:hidden and height between 0 and window height.

benoît
  • 1,445
  • 3
  • 13
  • 27