I'm trying to get it when the #name div is pressed, the page will scroll to #profile-pic but not having any luck.
$("#name").click(function() {
$('html, body').animate({
scrollTop: $("#profile-pic").offset().top
}, 2000);
});
http://jsfiddle.net/qv9f7p6u/1/
Because the scrollable part is a div, not the page. You need to select the div to scroll.
$('#left-panel').animate({
Related
I have a question about a tiny JS that I have put at the bottom of the page. The script scrolls to another div when the user clicks on one div. On my website there is a contact button (example below).
<div id="contactbutton"></div>
When the user of the website clicks on this div, the page scrolls down to another div on the page with a contact form on it (example below).
<div id="contactform">
<form>
(form content here)
</form>
</div>
I did that using the following JS code at the bottom of the page, that will scroll to the other div:
<script>
$("#contactbutton").click(function() {
$('html, body').animate({
scrollTop: $("#contactform").offset().top
}, 500);
});
</script>
The code works perfect, but problem is that I have a fixed div header on my website that is on top of everything on the page. When the user scrolls to #contactform, a part of this contact form div is behind this header.
I am looking for a way to scroll to #contactform. But instead of scrolling all the way, I want it to scroll till it's a certain amount of pixels away from the top of the browser window so it will show below the header and not behind it.
I hope somebody can help me out! Thanks alot in advance.
Elmigo
You want to substract the height of your sticky header from the scroll distance, something like this (#fixed-header is an id of your sticky header):
<script>
$("#contactbutton").click(function() {
$('html, body').animate({
scrollTop: $("#contactform").offset().top - $('#fixed-header').outerHeight()
}, 500);
});
</script>
You could substract the height of your header so the scroll will stop a few pixels before your content leaving some spaces for the header
<script>
$("#contactbutton").click(function() {
$('html, body').animate({
scrollTop: $("#contactform").offset().top - $('#header').height()
}, 500);
});
</script>
I have arrows in the center of my web pages at the end of sections and I was these to allow users to scroll to the next section on click. I have the following code where the first click works but subsequent clicks do not scroll even though the function is being called each time.
$('.scroll').on('click', function(event) {
alert('scroll');
$('html, body').animate({
scrollTop: $(".scroll").offset().top
}, 1000);
});
Can anyone assist? https://jsfiddle.net/avL459sm/2/
You should use current .scroll element you clicked on.
Look at this fiddle: https://jsfiddle.net/avL459sm/3/
Example URL
Here's my issue: When I resize the browser (approx. 320 px width), scroll down and click on the icon (green arrow) „next“ the scroll bar stays at the bottom of the page. What I want to achieve is something like
$(document).ready(function () {
$('html, body').animate({ scrollTop: 0 }, 'slow');
});
so the next page will autoscroll to top, but I've tried with no success. Is there any solution?
Thank you!
I'd add the animation to your (I guess) ajax success function.
success:function(e){
// load the new contents
setTimeout(function(){
$('html, body').animate({ scrollTop: 0 }, 'slow');
},50);
}
If it's not ajax you are using, add it to the button click event handler.
Try this code, I tested it on your site it's working there:
$('button[id^="question"]').click(function(){
$('#header-wrapper').animate({ scrollTop: 0 }, 'slow');
});
Scroll bar is on #header-wrapper element and not body. scrolling body wont do anything since it's already at 0 scroll height.
This is my jQuery code:
$(document).ready(function(){
$("#home-btn").click(function() {
$('html, body').animate({
scrollTop: $("#header").offset().top // Problem
}, 2000);
});
});
Can offset actually accept a value of bottom?? Since this is one of the links at the bottom of my page and I want to scroll from bottom to top. This brings in the top of the page for a second and then scrolls from bottom to top. How to make a smooth scroll effect?
You say it is a "link", perhaps you just need to prevent the default action of the link:
$("#home-btn").click(function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $("#header").offset().top
}, 2000);
});
Otherwise, it seems to work: jsfiddle
I have images that when clicked, resize. However, I would like it so that no matter how far the user scrolls, when an image is pressed the html and body will move to the top. I added an animation to the resize script but it seems to not be recognized.
$(".images img").on('click', function () {
$(this).toggleClass('selected');
$('html,body').animate({
scrollTop: $('.active').offset()
});
});
Code: http://jsfiddle.net/qSDP5/
EDIT*
I put a sticky header to the top of the page and used
$('html,body').animate({
scrollTop: $('#sticky').offset().top
});
But it doesn't seem to want to scroll to it.
It's because not only you don't have any element with the class of active, but also you are not defining the coordinate direction of that element you want to access, because of that your script doesn't scroll to that.
change this line :
scrollTop: $('.active').offset()
to this if you want to scroll to the top of the page
scrollTop: 0
or you can scroll to an element which resides in your html something like:
scrollTop: $('#header').offset().top
see this: http://jsfiddle.net/qSDP5/1/