Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    Hi,

    I am trying to modify a plugin for Wordpress called Sharebar (site|demo). It is a social bar that "floats" next to your entry content. I say float in quotations because it is just a position:absolute object (this removes it's physical mass and sticks it to the top of the container). As you scroll down, the object transitions to position:fixed, so that it stays at the side.

    What I want to change in this plugin is to make this bar not go past it's container; it should turn into position:absolute; bottom:0 as you scroll into the area beneath the entry content (the comments, the footer, etc).The reason I need this is because I am putting an indent into my entry content area, just like CNN.com does. It reduces the readable area to 550px (great for articles) and it ensures that the bar is tucked inside the body, rather than outside, which can pose problems for tablet screens.

    I don't really understand the javascript behind this functionality, so I am asking for some advice. Here is the javascript code. Again, I really just want it to become position:absolute; bottom:0 when it reaches the botto of my div.content. Any help would be appreciated.

    Code:
    /*
    *  ShareBar - Creates a dynamic, vertical sharing bar to the left of a WordPress post and hides it if browser window is too small
    *  Copyright 2010 Monjurul Dolon, http://mdolon.com/
    *  Released under the MIT, BSD, and GPL Licenses.
    *  More information: http://devgrow.com/sharebar
    */
    jQuery.fn.sharebar = function(options) {
        var defaults = {horizontal: true, swidth: 65, minwidth: 1000, position: 'left', leftOffset: 20, rightOffset: 10};
        var opts = jQuery.extend(defaults, options); var o = jQuery.meta ? jQuery.extend({}, opts, jQueryjQuery.data()) : opts;
    
        var w = jQuery(window).width();
        var sharebar = jQuery('#sharebar');
        var sharebarx = jQuery('#sharebarx');
        var parent = jQuery(sharebar).parent().width();
        var start = sharebar_init();
    
        function sharebar_init(){
            jQuery(sharebar).css('width',o.swidth+'px');
            if (o.position == 'left') jQuery(sharebar).css('marginLeft',(0-o.swidth-o.leftOffset));
            else {
                jQuery(sharebar).css('marginLeft',(parent+o.rightOffset));
            }
            if(w < o.minwidth && o.horizontal) jQuery(sharebarx).slideDown();
            else jQuery(sharebar).fadeIn();
            jQuery.event.add(window, "scroll", sharebar_scroll);
            jQuery.event.add(window, "resize", sharebar_resize);
            return jQuery(sharebar).offset().top;
        }
        function sharebar_resize() {
            var w = jQuery(window).width();
            if(w<o.minwidth){
                jQuery(sharebar).fadeOut();
                if(o.horizontal) jQuery(sharebarx).slideDown();
            }else{
                jQuery(sharebar).fadeIn();
                if(o.horizontal) jQuery(sharebarx).slideUp();
            }
        }
        function sharebar_scroll() {
            var p = jQuery(window).scrollTop();
            var w = jQuery(window).width();
            jQuery(sharebar).css('position',((p+10)>start) ? 'fixed' : 'absolute');
            jQuery(sharebar).css('top',((p+10)>start) ? '10px' : '');
        }
    
    };
    Attached Images Attached Images

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Ok, that last block is where the magic happens. After it sets the variables, it tells the element to switch to fixed after a number of pixels (calculated at the beginning) have scrolled off the screen. What you need to do is reverse that declaration once it gets to the bottom. However, there is no .scrollBottom, so you still have to calculate from the top. You'll do that by subtracting the window height from the document height and using that as the .scrollTop value.

    I helped someone with a similar problem using Codepen. If you want help with the actual implementation, I can do that, just not right now.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    Quote Originally Posted by Ronald Roe, post: 247589
    Ok, that last block is where the magic happens. After it sets the variables, it tells the element to switch to fixed after a number of pixels (calculated at the beginning) have scrolled off the screen. What you need to do is reverse that declaration once it gets to the bottom. However, there is no .scrollBottom, so you still have to calculate from the top. You'll do that by subtracting the window height from the document height and using that as the .scrollTop value.

    I helped someone with a similar problem using Codepen. If you want help with the actual implementation, I can do that, just not right now.
    Thanks Ronald. I've never used scrollTop before or actually done any calculative functions using JS (the scroll function on my website is 100% taken from somewhere). However, with your insight and your Codepen example, I should be able to figure it out on my own tomorrow. Might be a fun challenge. Thank you.

    -R


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:23 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com