简体   繁体   中英

Jquery smooth scrolling (not anchor)

I've seen a couple of articles here but I just can't find the right answer. I'm not looking for a smooth scrolling to an anchor point, I already got that, I'm looking for a smooth scrolling like this or this . somehow they both uses this jquery which I don't know how to implement.

I've tried this one: simplr-smoothscroll . But this one is not really that 'smooth', when you scroll fast it slows you down.

I've also tried: simple smooth-wheel . But I couldn't implement that properly, since my body height is set to 100%, someone else complained about that as well on github. Above that, the demo provided in the Download (not on github) didn't work for me aswell, tried to fix that demo but still no luck.

I've also try to use the source of that one: smoothwheel . However I'm not a javascript magician with javascript so, no couldn't get that done.

Here is the simple jsfiddle page I have, where it should work: jsfiddle

 <div id="menubar">
        <a href="#welcome">Welcome</a><a href="#welcome">Welcome</a><a href="#welcome">Welcome</a><a href="#welcome">Welcome</a>
    </div>
    <div id="container">
        <div id="content">
                <br />
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum a tortor fringilla, volutpat justo malesuada, suscipit justo. Fusce ante dui, bibendum non sapien vitae, hendrerit ultricies enim. Quisque dictum fermentum congue. Vestibulum posuere iaculis arcu, ultrices pharetra mauris ultrices finibus. Ut pulvinar sit amet justo et rhoncus. Maecenas id ante vel tortor vulputate commodo in in lacus. Nunc auctor vulputate ornare. Donec lacus odio, maximus ut massa a, finibus pulvinar justo. In facilisis quam sed pharetra aliquet. Etiam varius non eros non suscipit. Vestibulum venenatis placerat accumsan. Nulla nec consequat mauris. Etiam nisl magna, semper vel sem mollis, vehicula bibendum urna. Nunc porttitor dui sed volutpat imperdiet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pharetra ligula sit amet sapien mollis malesuada.

Sed mollis convallis risus. Duis volutpat, diam rutrum pretium scelerisque, odio nunc faucibus odio, sit amet cursus justo justo ac mi. Aenean laoreet et eros non viverra. Praesent id erat vel nisl molestie pharetra. Pellentesque eu eros nibh. Aliquam mollis, tortor eget consequat varius, metus sapien suscipit nisl, eu convallis ligula orci eu erat. Fusce et ex dignissim eros porta eleifend in quis sapien. Suspendisse potenti. Quisque non vehicula mauris, eget ornare tellus. Pellentesque aliquet massa augue, sed hendrerit massa condimentum eget. Nunc vulputate neque nunc, ac interdum velit tristique sed. Donec vestibulum nibh at tellus condimentum, sed rutrum quam pellentesque. Mauris congue blandit justo, a auctor est. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse et sem vulputate, malesuada lectus ultrices, luctus elit.

Ut enim metus, lobortis in malesuada sed, fringilla sit amet velit. Phasellus nec tincidunt risus. Nam id nulla et dolor tempor consectetur elementum sit amet lacus. Nunc non felis enim. Curabitur feugiat at quam ac auctor. Duis nibh enim, consequat id sagittis vitae, pharetra nec arcu. Pellentesque vitae nisl tincidunt purus faucibus dignissim ut egestas enim. Integer gravida suscipit interdum. Nunc ac tellus urna. Proin odio quam, pulvinar sed tristique sed, viverra sed mi. Phasellus dapibus ipsum in tempor aliquet. Nam a lectus eu purus volutpat sodales eu at ante. Duis quis scelerisque lectus. Mauris interdum massa quis nisi luctus tincidunt. Etiam ut varius arcu, venenatis lacinia nulla. Cras tristique porta accumsan.

Nam dignissim risus tellus, sit amet faucibus mi dignissim eget. Sed luctus dolor et augue convallis tristique. Etiam sed urna urna. Etiam id lacinia neque. Aliquam ac sapien lacus. Ut lacus quam, efficitur a orci a, gravida porttitor dui. Nunc eget augue vehicula, congue mi ultrices, tempus neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

Nullam at venenatis ante. Ut odio justo, auctor non lacinia at, sollicitudin vel augue. Pellentesque urna felis, posuere sit amet lacus fermentum, congue dignissim elit. Phasellus eleifend lectus vitae arcu tristique, et pellentesque nunc venenatis. Duis at est velit. Nullam vestibulum lorem auctor vestibulum ornare. Mauris sed libero ut libero pellentesque fermentum eget eu nisi. Nam augue dolor, mollis ut volutpat eget, vulputate sit amet nisl. Etiam at elit dapibus, laoreet est at, sagittis nisl. Pellentesque sed risus in quam pretium porta sed vitae velit. Phasellus risus odio, imperdiet vel vulputate eget, imperdiet eu nisl. Aliquam ultrices lorem nec aliquet consectetur.</p>
           <br />
      </div>
    <div id="img2">
    <img src="someImage.png" />
    </div>

css:

body {
min-width:800px;
margin: 0;
background-color:blue; <!--this is a image -->
background-size: 100% auto;
background-attachment:fixed;
height:100%;

}

#menubar{
position:fixed;
top:5px;
right:0;
background-color:none;
height:auto;
z-index:1;
}


#container{
position:absolute;
top:75%;
}

#content{
width:100%;
background-color:white;
margin:0;
}


#img2{
width:100%;
margin:0;
}

#img2 img{
width:100%;
}

In the example you gave, they are using jquery.smoothwheel by IntelliJ IDEA.

Check this link . Maybe it's the effect you are searching for.

Hope it's useful!

Edit by OP:

After @Academia gave me this fiddle I manged to get it to work for my needs.

This is what I ended up with:

css:

body {
    background: url(someImage.png); 
    background-size: 100% auto;
    background-attachment:fixed;
    margin: 0 auto;
    width:100%;
    height:1000px; /*key value to make this script work, else it only scrolls smoothly when your mouse is hovering over the #container*/
    overflow:auto;
    -webkit-overflow-scrolling: touch;
    position:relative;

}

#container{
     width:100%;
    background-color:white;
    position:absolute;
    top:700px; /*I intent to scroll the content over the bg image, that was my struggle, I had it in procentage first*/
}

#content{
    width:100%;
    background-color:white;
    margin:0;
}

html:

<body>   

    <div id="container">
        <div id="content">
<p>some content</p>
  </div>
 </div>
</body>

Now for the javascript, I had to add an function that would keep the top value of the container correct to the width of the screen

script for smoothscroll:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="js/jquery.smoothwheel.js"></script>

<script>

    $(document).ready(function () {
        $("body").smoothWheel()
    });

</script>

script for the top value of the container , 0.36 because 1920/700 = 0.36, this depends on the top value you want to have:

<script type="text/javascript">
   $(document).ready(function () {
   var jqeuryElem = jQuery("#container");
   var w = $(window).width();
   var top = w *0.36;

   jqeuryElem.css("top", top+"px");


   });
   $(window).resize(function () {
       var jqeuryElem = jQuery("#container");
       var w = $(window).width();
       var top = w * 0.36;

       jqeuryElem.css("top", top + "px");
   });
    </script>

I'll post the result when the website is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM