JavaScript Div Floating Plugin Introduction

Source: Internet
Author: User
As follows:
   
   
The code is as follows Copy Code
<div id= "wrapper" >
<div id= "Content" >

<p>this shows a sliding panel with no bottom boundary, and a bigger gap between the "top" viewport and the top of the Panel.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>

<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>

<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>
<p>bacon ipsum dolor sit amet do T-Bone Laborum, ad ground round turkey tip. Veniam Aliqua jowl, Duis in T-Bone Sint Tongue. Pork Loin tenderloin AD, Veniam chicken meatloaf.</p>

</div>


<div id= "sidebar" >
<p>hello, I ' m a sliding panel-unless the viewport is too small to contain me full glory, in which case I'll stay Right this so users can me.</p>
</div>
</div>
<script src= "<%: Url.content (" ~/scripts/portamento.js ")%>" ></script>
<script type= "Text/javascript" >
$ (' #sidebar '). Portamento ({gap:100}); Set a 100px gap rather than the default 10px
</script>
2.? CSS
The following:
  
  
  code is as follows copy code
<style type=" Text/css "
#wrapper {overflow:hidden; width:100%;}
#content {width:70%; margin-right:10px; float:left; min-height:1200px;}
#sidebar {width:200px; padding:10px; background: #f05b72; float:right; height:400px;}

#portamento_container {float:right; position:relative}/* Take the positioning of the sidebar, and become the STA RT point for the sidebar positioning */
#portamento_container #sidebar {float:none; Position:absolute}/* No need to F Loat anymore, become absolutely positoned */
#portamento_container #sidebar. Fixed {position:fixed;}/* If the panel is Sliding, it needs position:fixed */
</style>
3. Quoting Portamento.js
Portamento.js? as follows:
   
   
The code is as follows Copy Code
/*!
*
* Portamento v1.1.1-2011-09-02
* Http://simianstudios.com/portamento
*
* Copyright Kris Noble except where noted.
*
* Dual-licensed under the GPLv3 and Apache 2.0 licenses:
* http://www.gnu.org/licenses/gpl-3.0.html
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
/**
*
* Creates a sliding panel that respects the boundaries of
* A given wrapper, and also has sensible behaviour if the
* Viewport is too small to display the whole panel.
*
* Full documentation at Http://simianstudios.com/portamento
*
* ----
*
* Uses the Viewportoffset plugin by Ben Alman aka Cowboy:
* http://benalman.com/projects/jquery-misc-plugins/#viewportoffset
*
* Uses a portion of CFT by Juriy Zaytsev aka Kangax:
* http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED
*
* Uses Code by Matthew Eernisse:
* Http://www.fleegix.org/articles/2006-05-30-getting-the-scrollbar-width-in-pixels
*
* Builds on work by Remy Sharp:
* http://jqueryfordesigners.com/fixed-floating-elements/
*
*/
(function ($) {

$.fn.portamento = function (options) {

We ll use the window and document objects a lot
Saving them as variables now saves a lot of function calls
var Thiswindow = $ (window);
var thisdocument = $ (document);

/**
* Avoid namespace clashes by kris-included.
*
* JQuery viewportoffset-v0.3-2/3/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Copyright (c) "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
$.fn.viewportoffset = function () {
var win = $ (window);
var offset = $ (this). offset ();

return {
Left:offset.left-win.scrollleft (),
Top:offset.top-win.scrolltop ()
};
};

/**
*
* A test to the IF position:fixed is supported.
* Taken from CFT by kangax-http://kangax.github.com/cft/#IS_POSITION_FIXED_SUPPORTED
* Included as to avoid namespace clashes.
*
*/
function positionfixedsupported () {
var container = document.body;
if (document.createelement && container && container.appendchild && container.removechild) {
var el = document.createelement ("div");
if (!el.getboundingclientrect) {
return null;
}
el.innerhtml = "X";
El.style.cssText = "position:fixed;top:100px;";
Container.appendchild (EL);
var originalheight = container.style.height, originalscrolltop = container.scrolltop;
Container.style.height = "3000px";
Container.scrolltop = 500;
var elementtop = El.getboundingclientrect (). Top;
Container.style.height = OriginalHeight;
var issupported = Elementtop = 100;
Container.removechild (EL);
Container.scrolltop = Originalscrolltop;
return issupported;
}
return null;
}

/**
*
* Get the scrollbar width by Matthew Eernisse.
* Http://www.fleegix.org/articles/2006-05-30-getting-the-scrollbar-width-in-pixels
* Included as to avoid namespace clashes.
*
*/
function Getscrollerwidth () {
var scr = null;
var inn = null;
var wnoscroll = 0;
var wscroll = 0;

Outer scrolling Div
SCR = document.createelement (' div ');
scr.style.position = ' absolute ';
Scr.style.top = ' -1000px ';
Scr.style.left = ' -1000px ';
Scr.style.width = ' 100px ';
Scr.style.height = ' 50px ';
Start with no scrollbar
Scr.style.overflow = ' hidden ';

Inner Content Div
inn = document.createelement (' div ');
Inn.style.width = ' 100% ';
Inn.style.height = ' 200px ';

Put the inner div in the scrolling div
scr.appendchild (INN);
Append the scrolling div to the doc
Document.body.appendChild (SCR);

Width of the inner div sans scrollbar
Wnoscroll = Inn.offsetwidth;
ADD the scrollbar
Scr.style.overflow = ' auto ';
Width of the inner div width scrollbar
Wscroll = Inn.offsetwidth;

Remove the scrolling div from the doc
Document.body.removeChild (Document.body.lastChild);

Pixel width of the scroller
return (Wnoscroll-wscroll);
}

// ---------------------------------------------------------------------------------------------------

Get the definitive options
var opts = $.extend ({}, $.fn.portamento.defaults, options);

Setup the VARs accordingly
var panel = this;
var wrapper = Opts.wrapper;
var gap = Opts.gap;
var disableworkaround = Opts.disableworkaround;
var fullycapablebrowser = positionfixedsupported ();

if (panel.length!= 1) {
Die gracefully If the user has tried to pass multiple elements
(multiple element support is on the TODO list!) or no elements ...
return this;
}

if (!fullycapablebrowser && disableworkaround) {
Just stop here, as the dev doesn ' t want to use the workaround
return this;
}

Wrap the floating panel in a Div, then set a sensible min-height and width
Panel.wrap (' <div id= ' portamento_container '/> ');
var Float_container = $ (' #portamento_container ');
Float_container.css ({
' Min-height ': panel.outerheight (),
' Width ': panel.outerwidth ()
});

Calculate the upper scrolling boundary
var paneloffset = Panel.offset (). Top;
var panelmargin = parsefloat (panel.css (' margintop '). Replace (/auto/, 0));
var realpaneloffset = Paneloffset-panelmargin;
var topscrollboundary = Realpaneloffset-gap;

A couple of numbers to account for margins and padding on the relevant elements
var wrapperpaddingfix = parsefloat (wrapper.css (' paddingtop '). Replace (/auto/, 0));
var containermarginfix = parsefloat (float_container.css (' margintop '). Replace (/auto/, 0));

Do some work to fix IE misreporting the document width
var iefix = 0;

var Ismsie =/* @cc_on!@*/0;

if (Ismsie) {
Iefix = Getscrollerwidth () + 4;
}

// ---------------------------------------------------------------------------------------------------

Thiswindow.bind ("Scroll.portamento", function () {

if (Thiswindow.height () > Panel.outerheight () && thiswindow.width () >= (Thisdocument.width ()-Iefix)) {// Don ' t scroll if the window isn ' t big enough

var y = thisdocument.scrolltop (); Current scroll position of the document

if (y >= (topscrollboundary)) {//If we ' re at or past the upper scrolling boundary
if ((Panel.innerheight ()-Wrapper.viewportoffset (). Top)-Wrapperpaddingfix + Gap >= wrapper.height ()) {//If we ' re a T or past the bottom scrolling boundary
if (Panel.hasclass (' fixed ') | | thiswindow.height () >= panel.outerheight ()) {//Check that there ' s work to do
Panel.removeclass (' fixed ');
Panel.css (' Top ', (Wrapper.height ()-panel.innerheight ()) + ' px ');
}
else {//If we ' re somewhere in the middle
Panel.addclass (' fixed ');

if (fullycapablebrowser) {//supports position:fixed
Panel.css (' top ', Gap + ' px '); To keep the gap
} else {
Panel.clearqueue ();
Panel.css (' position ', ' absolute '). Animate ({top: (0-float_container.viewportoffset (). Top + Gap)});
}
}
} else {
If we ' re above the top scroll boundary
Panel.removeclass (' fixed ');
Panel.css (' top ', ' 0 '); Remove any added gap
}
} else {
Panel.removeclass (' fixed ');
}
});

// ---------------------------------------------------------------------------------------------------

Thiswindow.bind ("Resize.portamento", function () {
Stop users getting undesirable behaviour if they resize the window too small
if (Thiswindow.height () <= panel.outerheight () | | thiswindow.width () < Thisdocument.width ()) {
if (Panel.hasclass (' fixed ')) {
Panel.removeclass (' fixed ');
Panel.css (' top ', ' 0 ');
}
} else {
Thiswindow.trigger (' Scroll.portamento '); Trigger the scroll event to place the panel correctly
}
});

// ---------------------------------------------------------------------------------------------------

Thiswindow.bind ("Orientationchange.portamento", function () {
If device orientation changes, trigger the Resize event
Thiswindow.trigger (' Resize.portamento ');
});

// ---------------------------------------------------------------------------------------------------

Trigger the scroll event immediately so, the panel is positioned correctly if the page loads anywhere other than th E top.
Thiswindow.trigger (' Scroll.portamento ');

Return this to maintain chainability
return this;
};

Set some sensible defaults
$.fn.portamento.defaults = {
' Wrapper ': $ (' body '),//The element that would act as the sliding panel ' s boundaries
' Gap ': the gap (in pixels) left between the "top" of the viewport and the top of the panel
' Disableworkaround ': false//Option to disable the workaround for Not-quite capable browsers
};

}) (JQuery);
4. In JS?
  code is as follows copy code
        <script type= "Text/javascript" >
& nbsp;           $ (' #sidebar '). Portamento ({gap:100}); Set a 100px gap rather than the default 10px
        </script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.