Return to site

Smoothscroll Mac

broken image

In order to get better control over the smooth scroll and better cross browser support, you can opt for a smooth scroll with JS or Jquery method to come to the rescue.

How To Create Smooth Scroll With JS?

Smooth scrolling can also be done through JavaScript and it gives better control over the functionalities. For example, through a smooth scroll with CSS, I can apply the scroll behavior only where the scroll event is built in to be triggered like while clicking the web links. But with JavaScript, I can decide when to scroll and how much to scroll i.e. the offset of the element. In this section, I have modified the same code which I wrote above but here I use a smooth scroll with JS for scrolling to the links. For this, the following built-in JavaScript function is used:

window.scrollTo();

This function can be declared for smooth scroll in JS in two ways:

  1. window.scrollTo(x-coord, y-coord);
  2. window.scrollTo(options)

The x-coord parameter defines the pixel value in the horizontal direction to which the page needs to be scrolled.

The y-coord parameter defines the pixel value in the vertical direction to which the page needs to be scrolled.

Smoothscroll Mac License

The options field depends on the developer. This field is a dictionary parameter that contains a set of options or parameters defining the characteristics of the smooth scroll with JS. To remember this as a stepwise process, we perform the following process to achieve smooth scrolling with JS:

  1. Detect the element you want to connect scrolling to.
  2. Define how much you want to scroll the page.
  3. Trigger the scrolling property within the function.

If you remember these three steps, scrolling smoothly is a very easy part when it comes to the code. But, before taking reference from this code, ensure that you have deleted the following CSS line from the webpage given in the previous section code:

2
4

In the code given in the previous section, add the following code for smooth scroll in JS inside the page at any place:

Now we can relate the code with the steps that I wrote above:

Smoothscroll Mac Alternative

Detecting The Element:

2
constnavlink=document.querySelectorAll('.navbar a');
navlink.forEach(elem=>elem.addEventListener('click',smoothscroll));

In this code for smooth scroll with JS, I am selecting the elements with the class navbar and anchor tags that are within that class. Whenever you click such elements, a function will be triggered with the name smoothscroll.

Define How Much To Scroll:

In the function smoothscroll, I am defining the pixel value that needs to be scrolled. Since, this cannot be an absolute value, you can make use of the offsetTop function. So, this line returns the offset value for the element being referred to.

Trigger The Scrolling Property Within The function:

behavior: 'smooth'

With this code, you can trigger the property behavior and set it's value to 'smooth'. This means that you want to scroll smoothly.

This code will generate the same results as from JavaScript.

So with this, are we done with our options of creating a smooth scroll in the page? Well, no! Apart from CSS and JavaScript, we still have another method, smooth scroll with JQuery.

How To Create Smooth Scroll With JQuery?

Jquery is yet another method that gives us the liberty to implement smooth scrolling with even more control than JavaScript. Implementing a smooth scroll with Jquery allows us to control the time it should take to scroll to the target element. In other words, you can manage the speed of scrolling.

Delivery 32202. Before referring to the following code for a smooth scroll in Jquery, make sure you have no other code that influences the scrolling behavior of the web page.

Taking this snippet and connecting back to the steps I mentioned above, we get the following:

Detect The Element

In this code, I am selecting all the anchor tags located within the class named navbar. For all such elements, a function will be triggered when they are clicked.

Define How Much To Scroll

2
},1300

The above code for smooth scroll in Jquery calculates the offset of the attribute with 'hash' and returns the pixel value back.

The value 1300 refers to the time in milliseconds that I want the web page to take before reaching the target location i.e. window.location.hash.

This code for smooth scroll in Jquery generates the following output:

Wrapping It Up!

Scroll-behavior is the property of the CSS that allows a page to scroll smoothly or abruptly when a link is clicked (although this can be extended to any element via JS). This can be achieved through three methods that are through direct styling in the CSS, through JavaScript, and through JQuery. Although there are tons of plugins that are available in JavaScript which can also be used for similar results. Deciding which way to go depends totally on the web developer and the requirements.

If Opera and Safari are not in your browser matrix and you don't need more control over the scroll event, you can use the smooth scroll with the CSS method. But that is hardly the case. Every web developer wants to target a maximum audience and therefore I recommend using a smooth scroll with JS method, as it is simple and gives more control although smooth scroll with JQuery offers some more features on the plate.

You can use any of them as per your comfort level. Whatever the developer decides, it is always better to have a smooth scroll on your website. I have never seen a dissatisfied customer using the smooth-scroll property. So, it is better to learn than skip this property on your web page. Also, make sure that you perform cross browser testing over your browser matrix to ensure browser compatibility support of the feature.

If you have used something unique with scroll property, do comment below to help all the fellow developers learn and experiment more. I hope you liked this article on a smooth scroll with Jquery, JS, and CSS. Feel free to share this article with your peers on Twitter, LinkedIn, or any other social media of your choice. That's all for now. Happy Testing!!!😄

Written by Harish Rajora

I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way.

41277 Views | 12 Min Min Read

50654 Views | 6 Min Min Read

87002 Views | 13 Min Min Read

110712 Views | 27 Min Min Read





broken image