1

I have setup a simple 1 page website, and when you click each menu item the page smoothly scrolls to that specific section. That part I have working fine...

Once the section loads, an image is displayed, and text slowly scrolls up over the image, which works absolutely fine on the first section (highest section).

Issue I am having when you click the second, third and fourth menu link, once the page moves to that section of the page, the content carries on scrolling up due to the sections above loading the scroll also.

This is the javascript I am using to load the text to scroll up:

    $(document).ready(function() {

    /* Every time the window is scrolled ... */
    $(window).scroll( function(){

        /* Check the location of each desired element */
        $('.content-scroll').each( function(i){

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* Adjust the "200" to either have a delay or that the content starts fading a bit before you reach it  */
            bottom_of_window = bottom_of_window + 800; 

            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){

                $(this).animate({'margin-top':'0'},10000);

            }

        }); 

    });

});

CSS:

.content-scroll { margin-top: 1000px; }

Can anyone recommend the best way to do this, so that when I click the second, third and fourth section link it would jump to that section, and the content above loads instantly, without the content having to scroll up?

Appreciate any feedback. Thank you

EDIT: Beforehand I input the code above for each link (.about-section, services-section etc), but realises I only needed to input it once.

Still, the content within scrolls up once the section is loaded.

Any feedback appreciated :)

EDIT 2: I have put together a test website, showing the scroll issue http://test.flixonstudios.co.uk - here you can see what I mean it clear detail.

jpopuk
  • 21
  • 4
  • your say in your code check the location of each and use `$('.about-scroll')` but you then also mention that they are named differently. services-scroll, .shop-scroll, .contact-scroll, so that is only going to be checking for the about-scroll, also this is a scroll event and you mentioned when you click on an element, so its activating every time its scrolls which cna cause a big issue??? Could you possibly add more details and your html etc so we can debug further, – Simon Davies Aug 09 '17 at 23:45
  • @SimonDavies the code is input 4 times (probably not the best way), but that's how I have it setup at the moment, which seems to work in terms of the content scrolling up, but as I mentioned, when you click the second, third or fourth link the content above scrolls up, so the content keeps moving once you reach one of those specific sections. – jpopuk Aug 09 '17 at 23:47
  • I think that sbecause its within the SCROLL event, whcih is triggered each time the window is scrolling, so it will constantly scroll and activate the code , it probably need to be looked at and redone? have alook at this https://stackoverflow.com/questions/18071046/smooth-scroll-to-specific-div-on-click – Simon Davies Aug 09 '17 at 23:49
  • basically you need to attach a click event on each link then call the scroll function not the scroll event. see muy post – Simon Davies Aug 09 '17 at 23:50
  • @SimonDavies - that link is regarding smooth scrolling? As I stated, that part is fine as when you click the link, it scrolls to the desired section... it's just when it reaches that section, the content then keeps scrolling up... I understand that I am not doing this the best way, and that I was I am hoping for some advice on a better method... appreciate your feedback :) – jpopuk Aug 09 '17 at 23:55
  • i think your problem is the content being within the `$(window).scroll( function(){` because it triggers when the screen scrolls, so when you click it not only scrolls, but then activated the scroll events that then will activate the `$('.about-scroll').each` and as the about is the forst one it scrolls back to the about, anyway i have pull an example of what ive demoed before – Simon Davies Aug 10 '17 at 00:01

1 Answers1

0

I have this codepen i did a while back

https://codepen.io/simondavies/pen/WRXOZw

$('#navigation').on('click', function(evt){
  evt.preventDefault();
  var gotoSection = evt.target.dataset.scrollto;
  var scrollPos = document.querySelector('#'+ gotoSection).offsetTop;
  $('html,body').animate({scrollTop: scrollPos },1000,'linear');
  
});

$('p').on('click','a.back',function(evt){
  evt.preventDefault();
  $('html,body').animate({scrollTop: 0 },1000,'linear');
  
})
.sections {
  margin: 0 auto;
  padding:30px;
  height: 100Vh;
  background: #d9d9d9;
   display: table;
  verticle-align: middle;
}
.two {
  background: #e9e9e9;
}
p {
  margin: 0 auto;
  text-align: center;
  font-family: helvetica;
  font-size: 20px;
  line-height: 30px;
  font-wieght: normal;
  display: table-cell;
  vertical-align: middle;
  height: 100%;
}
nav {
  margin: 0 auto;
  padding-top: 10px;
  text-align: center;
  width: 100%;
  height: 40px;
  position: fixed;
  z-index: 10;
  top: 0;
  background: #555;
}
a {
  margin: 0 auto;
  text-align: center;
  font-family: helvetica;
  font-size: 20px;
  line-height: 30px;
  font-wieght: normal;
  display: inline-block;
  height: 30px;
  width: 200px;
  text-decoration: none;
  color: #fff;
  transition: color 500ms;
}

a:hover {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="navigation">
<a href="#" data-scrollto="section-one">Section One</a> | <a href="#" data-scrollto="section-two">Section Two</a> | <a href="#" data-scrollto="section-three">Section three</a>
</nav>
<div class="sections" id="section-one">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Natus neque, ducimus pariatur odio architecto excepturi, enim non recusandae, temporibus facilis eveniet nulla quo laudantium, alias quos in aliquam delectus dolore!<br /><br /><a href="#" class="back">Back to top</a></p>
</div>
<div class="sections two" id="section-two">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Natus neque, ducimus pariatur odio architecto excepturi, enim non recusandae, temporibus facilis eveniet nulla quo laudantium, alias quos in aliquam delectus dolore!<br /><br /><a href="#" class="back">Back to top</a></p>
</div>
<div class="sections" id="section-three">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Natus neque, ducimus pariatur odio architecto excepturi, enim non recusandae, temporibus facilis eveniet nulla quo laudantium, alias quos in aliquam delectus dolore!<br /><br /><a href="#" class="back">Back to top</a></p>
</div>
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
  • Although that is a useful bit of code, I think you might be missing the point... the text content scrolls up whilst the section has already loaded. So when you click on one of the links, it will smoothly jump to that section, and displays an image, with the text slowly scrolling up... so when you click the second, third or fourth link it jumps to that section, and then the above content is still scrolling up, causing that section to scroll up... hopefully that makes some sense :) Cheers – jpopuk Aug 10 '17 at 00:02
  • So it's the inner content that's scrolling. Still, think it might be the scroll event? But with out a more in-depth example or imagery i'm at a loss sorry – Simon Davies Aug 10 '17 at 00:08
  • yes, that is correct the inner content is scrolling up, so for instance the About us text is scrolling up from the bottom of the screen once the about link has been clicked, and about section loads... It's a pity I cannot show you an example of what I mean :( - appreciate your help though. – jpopuk Aug 10 '17 at 00:13
  • not a problem, have you tried using the embed code in here option, to add a working example of what you're trying to do, that way could be easier to debug. Or post all that you have etc. Still think it's that scroll event :-) im ahving to go now though hope its srted. – Simon Davies Aug 10 '17 at 00:17
  • I have put together a test website, showing the scroll issue http://test.flixonstudios.co.uk/ - here you can see what I mean it clear detail... still no resolution with this one as of yet! :( – jpopuk Aug 10 '17 at 17:16