5 Beautiful & Simple jQuery snippets for Drupal
15 Jul
Jarkko Oksanen
We all know that there is an abundance of Javascript and jQuery libraries out there that provide all kinds of interesting functionalities for almost every issue you could think of. During the last few years of working on various projects I have gathered snippets, scripts and plugins that I am reusing when the need comes up. This article will demonstrate some of the better and easily re-usable ones.
1. Parallax Scrolling backgrounds
The parallax effect has been a nice thing to have for a while now and is especially useful in brochure sites or one page websites. However it has its uses in other contexts as well. I came across a simple function to apply the effect to css backgrounds awhile back. Notice that it only works on backgrounds, not on other elements.
Snippet:
- // Parallax scroll.
- $.fn.parallax = function(options) {
- var windowHeight = $(window).height();
- // Establish default settings.
- var settings = $.extend({
- speed : 0.15
- }, options);
- // Iterate over each object in collection.
- return this.each( function() {
- // Save a reference to the element.
- var $this = $(this);
- // Set up Scroll Handler.
- $(document).scroll(function(){
- var scrollTop = $(window).scrollTop();
- var offset = $this.offset().top;
- var height = $this.outerHeight();
- // Check if above or below viewport.
- if (offset + height < scrollTop || offset > scrollTop + windowHeight) {
- return;
- }
- var yBgPosition = Math.round((offset - scrollTop) * settings.speed);
- // Apply the Y Background Position to Set the Parallax Effect.
- $this.css('background-position', 'center ' + yBgPosition + 'px');
- });
- });
- };
Use the speed setting to set the speed of the parallax effect.
And to simply use it:
- $('.background').parallax({
- speed : 0.3
- });
This is simple enough to use as a part of your Javascript scripts instead of using a library.
Jsfiddle with a gradiant to display effect. Use with an image for optimal demonstration: http://jsfiddle.net/jOksanen/7ywhcdd2/5
For more complex usage, the library that this work is based on is located at: http://pixelcog.github.io/parallax.js/
2. Floating block
Sometimes you just want to “fix” something on your website after scrolling the page for a bit. This could be a login block, your main menu or anything else.
You can use a plugin https://github.com/somewebmedia/hc-sticky to implement this behavior.
Snippet (only with the plugin installed):
$('#element').hcSticky();
D7 module that implements the behavior: https://www.drupal.org/project/floating_block
3. Smooth Scrolling to anchor links
A smooth scrolling behavior to anchor links on page is an effect that is both nice and simple. Its as simple as it sounds and gives you an animation on anchor link clicks.
Demonstration: https://css-tricks.com/examples/SmoothPageScroll/
Snippet:
- $(function() {
- $('a[href*=#]:not([href=#])').click(function() {
- if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
- var target = $(this.hash);
- target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
- if (target.length) {
- $('html,body').animate({
- scrollTop: target.offset().top
- }, 1000);
- return false;
- }
- }
- });
- });
D7 modules that implement the behavior: https://www.drupal.org/sandbox/souless/2349477 or https://www.drupal.org/project/scroll_to_destination_anchors
4. Back to top
A small effect that makes blogs and websites more user-friendly. A button that appears and scrolls back to the top of the page that you’re currently on. Especially for mobile devices its a great thing to have. This can be used in conjunction with the last snippet to create a nice smooth scroll back to the top.
Snippet:
- function back_to_top(selector){
- $(window).scroll(function () {
- if ($(this).scrollTop() > 50) {
- $(selector).fadeIn();
- } else {
- $(selector).fadeOut();
- }
- });
- $(selector).click(function () {
- $(selector).tooltip('hide');
- $('body,html').animate({
- scrollTop: 0
- }, 0);
- return false;
- });
- };
- // Call this function on your button selector.
And then you only need to create a button on your page somewhere and apply the function with the collect selector.
And of course a bit of css to make the button fixed.
- .top {
- cursor: pointer;
- position: fixed;
- bottom: 25px;
- right: 25px;
- display: none;
- }
Drupal modules for the behavior: https://www.drupal.org/project/back_to_top and https://www.drupal.org/project/scroll_to_top
Demonstration and original source: http://bootsnipp.com/snippets/featured/link-to-top-page
5. Keep your footer at the bottom of the page
On many sites that I’ve worked with I’ve run to the issue that the page is shorter than the actual page. Keeping the footer at the bottom at all times can be accomplished by using perfect markup and css (https://css-tricks.com/snippets/css/sticky-footer/), but in some cases some javascript is needed due to time or markup limitation concerns.
Snippet:
- var footerHeight = 0,
- footerTop = 0;
- function positionFooter(footer) {
- footerHeight = footer.height();
- footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";
- if ( ($(document.body).height()+footerHeight) < $(window).height()) {
- footer.css({
- position: "absolute"
- }).animate({
- top: footerTop
- })
- } else {
- footer.css({
- position: "static"
- })
- }
- }
- positionFooter(".your-footer");
This snippet places the footer at the bottom of the page. It should be called on window resize to make the effect perfect. The snippet is a simplified version of what's presented in the link below.
Demonstration and original source: https://css-tricks.com/snippets/jquery/jquery-sticky-footer/
drupal planet