Xe Blog

  • Home
  • Ebooks
    • Design & Illustration
    • Code
    • Web Design
    • General
  • Learning
    • Design & Illustration
      • Photoshop
      • Illustrator
      • Corel Draw
    • Code
      • Web Development
      • Wordpress
      • PHP
      • JavaScript
    • Web Design
      • HTML-5
      • CSS-3
      • Portfolios
    • Computer Skills
      • Windows
      • Linux
      • Terminal
      • Hardware
    • Office
      • MS-Access
      • MS-Excel
      • MS-PowerPoint
      • MS-Word
    • General
      • Urdu Inpage
      • Tips & Tricks
      • Security
  • Software
    • Antivirus
    • CD DVD
    • Converters
    • Drivers
    • Dictionary
    • Graphics
    • Media Player
    • Office
    • Windows OS
    • Linux OS
  • Games
    • Action
    • Fighting
    • GTA
  • Social
    • Facebook
    • Twitter
    • YouTube
    • Instagram

Saturday, 10 October 2015

jQuery topLink Plugin

Posted by Unknown at 21:35 Labels: code , javascript , jQuery

Last week I released a snippet of code for MooTools that allowed you to fade in and out a "to the top" link on any page. Here's how to implement that functionality using jQuery.

The XHTML

<a href="#top" id="top-link">Top of Page</a>
A simple link.

The CSS

#top-link { display:none; position:fixed; right:5px; bottom:5px; color:green; font-weight:bold; text-decoration:none; border:1px solid green; background:Lightgreen; padding:10px; }
A little CSS for position and style.

The jQuery JavaScript

//plugin
jQuery.fn.topLink = function(settings) {
 settings = jQuery.extend({
  min: 1,
  fadeSpeed: 200
 }, settings);
 return this.each(function() {
  //listen for scroll
  var el = $(this);
  el.hide(); //in case the user forgot
  $(window).scroll(function() {
   if($(window).scrollTop() >= settings.min)
   {
    el.fadeIn(settings.fadeSpeed);
   }
   else
   {
    el.fadeOut(settings.fadeSpeed);
   }
  });
 });
};

//usage w/ smoothscroll
$(document).ready(function() {
 //set the link
 $('#top-link').topLink({
  min: 400,
  fadeSpeed: 500
 });
 //smoothscroll
 $('#top-link').click(function(e) {
  e.preventDefault();
  $.scrollTo(0,300);
 });
});
You'll see that I've added jQuery's ScrollTo plugin to add some smoothness to the anchor.
Please note that this version doesn't work with Internet Explorer due to IE's lack of CSS "position:fixed" support. Here is a shotty attempt at IE support:
//plugin
jQuery.fn.topLink = function(settings) {
  settings = jQuery.extend({
   min: 1,
   fadeSpeed: 200,
   ieOffset: 50
  }, settings);
  return this.each(function() {
   //listen for scroll
   var el = $(this);
   el.css('display','none'); //in case the user forgot
   $(window).scroll(function() {
    //stupid IE hack
    if(!jQuery.support.hrefNormalized) {
     el.css({
      'position': 'absolute',
      'top': $(window).scrollTop() + $(window).height() - settings.ieOffset
     });
    }
    if($(window).scrollTop() >= settings.min)
    {
     el.fadeIn(settings.fadeSpeed);
    }
    else
    {
     el.fadeOut(settings.fadeSpeed);
    }
   });
  });
 };
Tweet

No comments :

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom )

Blog Archive

  • 2015 (11)
    • October (11)
      • How To Create A WordPress Plugin
      • WordPress Custom Post Type Complete - Easy Way
      • How to Create CSS Sliding Background Effect
      • 10 PHP Tips Every W.Developer Should Know :)
      • Flip Wall With jQuery & CSS
      • 7 Essential, Most Important JavaScript Functions
      • jQuery topLink Plugin
      • Layers vs. Artboards: ADOBE ILLUSTRATOR
      • Regular Expressions ~ Hands On!
      • Clipboard.js makes it easy to copy and cut text fr...
      • Unraveling the Secrets of WordPress' Comments.php ...
  • 2014 (2)
    • December (2)

© Xe Blog 2013 . Powered by Bootstrap Blogger templates and RWD Testing Tool