Position:fixed horizontal scrolling by using CSS styles _jquery

Source: Internet
Author: User
Using the CSS style "position:fixed" allows the div block to be fixed in a fixed position, even if there is a scroll bar that does not change its position. Position:fixed has been a stunning effect for many developers, but when the horizontal scroll bar appears, the effect is not so easy to accept. Sometimes we hope that when the horizontal scroll bar appears, the DIV block can move around with the scroll bar to achieve vertical fixed positioning (fixed), horizontal relative positioning (absolute). This article provides a workaround, with jquery extension source.

The implementation of this article is to use JS to control the DIV block scrolling horizontally with the scroll bar, the principle is that when the Window object occurs scroll event and resize event, update the left or right value of the div block, so that it relative to the browser on the left-hand side of the position and immediately change. The div block needs to be position:fixed style decorated.

When the div block is fixed horizontally relative to the right of the browser, when the Window object occurs scroll or resize event, its right style value is updated, and the value should be:
Copy Code code as follows:

var new_right = ($ (window). ScrollLeft () + $ (window). Width ()-$ (document). Width () + original_right) + ' px '

When the div block is fixed horizontally relative to the left of the browser, when the Window object occurs, the scroll or resize event is updated, and the value should be:
Copy Code code as follows:

var new_left = (Original_left-$ (window). ScrollLeft ()) + ' px '

The Original_left and original_right that appear in the above code are the left and right values of the original div block. The full jquery extension code is as follows:
Copy Code code as follows:

(function ($) {
$. scrollfixed = function (el, options) {
var base = this;
Base. $el = $ (EL);
Base.el = El;
var target = base. $el;
var original_left = parseint (Target.css (' left '));
var original_right = parseint (Target.css (' right '));

var _fix_position = function () {
if (base.options.fixed = = ' right ') {
Target.css (' Right ', ($ (window). ScrollLeft () + $ (window). Width ()-$ (document). Width () + original_right) + ' px ');
else if (base.options.fixed = = ' Left ') {
Target.css (' Left ', (Original_left-$ (window). ScrollLeft ()) + ' px ');
}
};

var windowresize = function () {
_fix_position ();
};

var windowscroll = function () {
_fix_position ();
};

Base.init = function () {
Base.options = $.extend ({}, $. Scrollfixed.defaultoptions, Options);
$ (window). Resize (windowresize);
$ (window). Scroll (Windowscroll);
_fix_position ();
Console.log (base.options.fixed);
};

Base.init ();
};

$. Scrollfixed.defaultoptions = {
Fixed: ' Left '
};

$.fn.scrollfixed = function (options) {
Return This.each (function () {
(New $. Scrollfixed (this, options));
});
};
}) (JQuery);

Use instance:
Copy Code code as follows:

$ (' #leftsider '). Scrollfixed ({fixed: ' left '});
$ (' #rightsider '). Scrollfixed ({fixed: ' right '});
Related Article

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.