Easing href to div/section of website - javascript

I am not sure how to mimic this (http://ironsummitmedia.github.io/startbootstrap-agency/)
website's easing. What I mean by easing is when I click on the websites navigation tabs, I would slide to the appropriate portion of the section/div. I think it uses jQuery UI easing (http://api.jqueryui.com/easings/). I'm not sure where to start as I just start learning jQuery and front end development.
I think to get started, I would do
$("#nav-pill").onclick(function() {
// easing methods?
})

You have syntax error in your jQuery
$("#nav-pill").on('click', function() { });
And the following code makes you achieve the smooth scrolling effect while
jQuery code:
$("#nav-pill").on('click', function() {
var mode="easeInOutCirc"; // or select the mode from jQuery easings
$('html, body').animate({
scrollTop: $("#myDiv").offset().top
}, 2000, mode);
});
It should work just fine for your case.

For this effect you can use animate
Example:
$("#nav-pill").click(function(){
$('html, body').stop().animate({
scrollTop: $("#container").offset().top
}, 1500,'easeInOutExpo');
});
Or you can use ScrollTo plugin (http://demos.flesler.com/jquery/scrollTo/)

Related

Asp.net Core JQuery scroll to div on page load not working

I have run up against a little problem. I am trying to get the page to scroll to a specific point on page load but I'm not getting anywhere. The code I have used is below. For some reason it doesn't seem to be running at all.
<script type="text/javascript">
// scroll past header on page load
function scrollToDiv() {
$(document).ready(function () {
var ele = $('html, body').animate({ scrollTop: $('#tree').position().top }, 500, 'linear');
});
};
scrollToDiv();
</script>
If you have any suggestions as to why it is not working I would certainly appreciate it.
I am building using ASP.NET Core. The above jQuery code worked on an old webforms project so I have no idea why it isn't working now.
To do this with jQuery:
<script>
$(function(){
const top = $('#tree').position().top;
$('html, body').animate({ scrollTop: top }, 500, 'linear');
});
</script>
However, you don't need jQuery for this:
<script>
window.addEventListener('DOMContentLoaded', () => {
const target = document.getElementById('tree');
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
});
</script>
Window: DOMContentLoaded event - Web APIs | MDN
Element.scrollIntoView() - Web APIs | MDN

Scroll to id href without #

Hi I'm developing a site and I'd like to make people scroll to an href id without showing it in the navigation bar of their browsers. If is it possible I'd like to do it with
Am I able to do it? Thanks
Example: index.html#id
How I want it: index.html
You can use Element.scrollIntoView to achieve this. The javascript needed is
var scrollToElement = function(id){
document.getElementById(id).scrollIntoView();
}
If you pass in {behaviour: 'smooth'} to the function you get smooth scrolling which is really slick.
Check https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView for further documentation on this function
pure JS version
Using window.scrollTo(x,y) you can move the viewport:
var elem = document.getElementById("your element id");
window.scrollTo(0, elem.offsetTop);
jQuery Version
If you are already using jQuery, you can use .animate() for a smooth scroll transition to the element.
Here is an example:
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 2000);
});
See also here

Fade effect with .hide() .show() in JQuery

I found a nice method to split large post, on blogger, into multiple pages here: http://blogtimenow.com/blogging/splitting-long-blog-post-blogger/
I'm trying to add a fade effect when switching pages, but i don't know how to do this, i have no experience when it comes to jquery...
I managed so far to make the page scroll back to the top, and added some animations using time values, but it's not looking quite right.
jQuery(document).ready(function(){
jQuery('.page1').click(function(){
jQuery("html, body").animate({ scrollTop: 0 }, "slow");
jQuery('.content1').show(2000);
jQuery('.content2').hide(1000);
jQuery('.content3').hide(1000);
jQuery('.content4').hide(1000);
jQuery('.content5').hide(1000);
So basically what i want is the current "page" to fade out and the next "page" to fade in...
Use below code
below javascript will add effect fadeIn-fadeOut on page while switching the pages.
<script type="text/javascript">
$(document).ready(function() {
$("body").fadeIn(800);
$("a").not(".no-fade").click(function(event){
event.preventDefault();
linkLocation = this.href;
$("body").fadeOut(400, redirectPage);
});
// Redirects page
function redirectPage() {
window.location = linkLocation;
}
});
</script>
Put above code into <head> tag in your Html page
Add this code
jQuery(document).ready(function(){
jQuery('.page1').click(function(){
jQuery("html, body").animate({ scrollTop: 0 }, "slow");
jQuery('.content1').fadeIn(2000);
jQuery('.content2').fadeOut(1000);
jQuery('.content3').fadeOut(1000);
jQuery('.content4').fadeOut(1000);
jQuery('.content5').fadeOut(1000);

How to combine two similar jQuery in header (scrollTo - smooth)

I managed to create scroll to effect on a testing web-page. At first I had
(function($) {
$(document).ready(function() {
$('html, body').animate({
'scrollTop': $('#static').offset().top
}, 1500);
});
})(jQuery);
which smoothly went on every page open to that anchor "static". Because header is big on page. so beside index page every navigation link would scroll past header down to anchor. That works perfect. But then I decided to make some submenu items. and they can't work because I use
<script type="text/javascript">
$(document).ready(function(e){
var str= location.hash;
var n=str.replace("_temp","");
$('html,body').animate({scrollTop:$(n).offset().top}, 500);
});
for that. This script can scroll down to anchor named "#something" on different page even and still smoothly scroll down. I found both scripts searching on Stack Overflow.
Problem is that when I use both of these, only 1st one works. They are similar so that's the problem. Is there any way to make them both work. If there is anchor "static" use first, if not use second?
How about
$(document).ready(function(e){
var str= location.hash;
var n=str.replace("_temp","");
if(n != "static") {
$('html,body').animate({scrollTop:$(n).offset().top}, 500);
} else {
$('html, body').animate({ 'scrollTop': $('#static').offset().top}, 1500);
}
});

Tweak smooth scrolling code

I am trying to learn jQuery and I'm having a mental blank at the moment. The following code scrolls my page to the top with a smooth transition, but I would like this smooth transition to work for all anchor/ID links on my site (it's only a one pager).
$(document).ready(function() {
$('a[href="#the-top"]').click(function (e) {
$("html, body").animate({ scrollTop: $('#the-top').offset().top }, 1000);
return false;
});
});
How can I change my code to achieve this?
jQuery(function($) {
$('a[href^=#]').bind('click', function (evt) {
var $what = $('#' + $(this).attr('href').split('#')[1]);
$('html, body').stop().animate({ scrollTop: $what.offset().top }, 1000);
evt.preventDefault();
});
});
Changes suggested in this code:
Change global $ object to jQuery
Just jQuery(fn) as document.ready(fn)
Closure: use jQuery as $ inside that function
Prevent default event from anchor instead of return false (source: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/)
Use of $what asking for the #something part of anchor href, in order to prevent misbehaviors in IE (because if you have href="#some" sometimes it become href="http://yoursite.com/yourcurrentpage/#some instead)
All of these are kind of optional. You get the idea. Feel free to change.
DEMO AT: http://jsfiddle.net/Nm3cT/
Take a look at Chris Coyier's smooth Scrolling script. It does exactly that and needs no further configuration. Plus, it updates the address on the browser.

Categories