mCustomScrollbar doesn't seem to work for horizontal scrollbars? - javascript

http://jsfiddle.net/3LQJG/2/
Above is a JSFiddle demonstrating the problem. I invoke the mCustomScrollbar on the parent of the content holder which has overflow in the x direction.
However, you will notice that it modifies the width of the container, and the scrollbar is inactive.
How do I correct this?

Look at this example. Look at source code of mCustomScrollbar demo page and you will see that horizontal scrollbar requires:
axis:'x'
advanced:{autoExpandHorizontalScroll:true} - I don't know what is it for, but without it scrollbar does not work
Also, I've prepared example of jQuery Scrollbar - just to look and try. Scrollbar style is based on container class. Different scrollbar types are available on demo page

Thank you for your correct answer! I was wondering why the horizontal scroll was not working.
I modified a little bit as follows:
$(function(){
var csObj = new Object();
//csObj.horizontalScroll=true;
csObj.setWidth=700;
csObj.setHeight=300;
csObj.verticalScroll=true;
csObj.axis="xy";
csObj.advanced={autoExpandHorizontalScroll:true};
csObj.scrollButtons = {
enable:true,
scrollType:"pixels",
scrollAmount:300
};
csObj.callbacks = {onTotalScroll:finishFunc};
//csObj.theme="dark";
$("#content_1").mCustomScrollbar(csObj);
});
I think this style is much easier for better understanding.

Related

Dealing with scroll bars and jquery .width() method

jQuery's .width() method doesn't seem to account for scroll bars. This is problematic for me, since I'd like to set the width of some children to equal the width of their parent. I used jQuery similar to the following:
$('#contentDiv').width($('#containerDiv').width())
In this example, #contentDiv is the element I'd like to size, and I want to set it to have the width of #containerDiv, which is its parent element. My problem is that this cuts off the side of #contentDiv, as seen in this fiddle.
In my actual code, I have several elements that I'm sizing with jQuery, which all need to fit in the scrollable div, so just setting the css of #contentDiv to 100% is not an option. What's the best way of dealing with scroll bar widths of divs in jQuery?
The best solution I found while working around this solution is this:
http://chris-spittles.co.uk/?p=531
jQuery is all powerful and everything but sometimes a small dash of native JS is all you need to render pixel perfect pages... I hope you will find this solution helpful!
UPDATED:
None of the jQuery width-finding methods account for the scroll bar. In my original example, using .innerWidth(true) LOOKS like it works, but only because it returns and object, which causes width to fail and the inner contents size themselves to fit in the available space, because the example wasn't very good. However, it's possible to write a function to compute the available space in a div with a scroll bar in it, which can then be used to position the contents as you wish.
To write that function, I took advantage of the fact that, when a div is appended to a div with a scroll bar in it, it takes up the full available width (i.e. the inner width of the parent minus the width of the scroll bar).
The function looks like this:
function noScrollWidth(div){
var measureDiv = $('<div id="measureDiv">');
div.append(measureDiv);
var width = measureDiv.outerWidth();
measureDiv.remove();
return width
};
I then use this to size my content div:
$('#contentDiv').width(noScrollWidth($('#containerDiv')));
Working fiddle.
Try this:
$('#contentDiv').width($('#containerDiv')[0].clientWidth)
For more information about that solution, see this StackOverflow answer.
Another approach I'd try is setting both elements' box-sizing property to 'border-box', and see whether setting your contentDiv's width to 100% then works the way you want.
Now that fewer projects worry about crufty old browsers anymore, 'border-box' can make things easier to work with. Be sure to test multiple browsers on multiple platforms, though, because I'm not sure they all handle scrollbars the same way.

Execute function when div height changes

I have a div that changes in height based on the content (its width is fixed).
I want to execute a function each time this automatic change in div height occurs.
Can someone please help me achieve this?
Thanks in advance!
EDIT:
The div height changes when I drag and drop some content on the div. (I am using JQuery UI draggable-droppable). So, the change in height is user initiated.
I found an excellent solution here, created by the Filament group: http://filamentgroup.com/lab/setting_equal_heights_with_jquery/ - please do not click this; it seems the resource has been removed.

Centering background image javascript slider

I have this up and running and was wondering if you can start the image in the center rather than to the left. Seems if I manipulate the css it doesn't center it so wondered if it needs something within the javascript. Or in fact even to select a specific starting point within the image. My javascript knowledge is not the greatest though...
here's what I'm talking about
http://jsfiddle.net/ARTsinn/MvRdD/890/
You can use the jQuery scrollLeft() function onload to scroll the container to the center. Try this:
var center = $('#content').width()/2 - scrollWrapper.width()/2
scrollWrapper.scrollLeft(center);
Demo: http://jsfiddle.net/MvRdD/922/
The following will center the image, with no scrollbars visible:
newleft =$('body').width()/2 - $('#content').width()/2;
$('#content').offset({left: newleft});
Unfortunately it's not perfect as it doesn't let you scroll to leftest part by pressing your 'Left' button, I'm trying to see if this can be improved but nonetheless this can be useful for your as fiddling upon it.
See demo

div container following scroll of screen

I'd like to set up a "backward-compatible" scrolling sidebar on one of my pages.
I have a page containing information about a species of fish which can be extraordinarily long and images to accompany it.
The images are in the right-hand pane at the moment and I'd like them to follow the user as they scroll to the bottom of the page.
I've used the following code with some success:
jQuery().ready(function($) {
var $scrollingDiv = $("#sidebar");
$(window).scroll(function(){
$scrollingDiv
.stop()
.animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" );
});
});
But it jumps too far when scrolling down.
(original position)
(scrolled a single mousewheel click)
When you start scrolling down the page, the sidebar appears around half-way down and as such you can only see two of the images.
Once a user scrolls past X point (say 400px), I would like the sidebar to start moving with the page. However, it also needs to go back to its original position when the user reaches the top of the page once more.
Is there a fix that can be applied to this code, or a better way of approaching this?
---------------------------------------------------------------------------------
EDIT: position:fixed Problem
When I try to apply position:fixed as per Josh and David's suggestions (either bit of JS code), this happens:
Here is Firebug's read-out of the CSS styles attached to this element:
You can use a plugin for this, but it’s such a simple task that you might as well do it on your own.
Consider this simple markup:
<div id="content">Content</div>
<div id="sidebar"><div>Sidebar</div></div>
And this would be all the javascript you need:
var el = $('#sidebar'),
pos = el.position().top;
$(window).scroll(function() {
el.toggleClass('fixed', $(this).scrollTop() >= pos);
});
Now, it will add a class to the #sidebar div as soon as the user scroll further than the sidebar is positioned, so all you need now is som CSS. I’m applying the fixed positioning to a child element to avoid layout problems:
#sidebar.fixed > div{position:fixed;}
I put up a really simple demo here: http://jsfiddle.net/QZyH3/
You should try the jQuery code found in this tutorial, which replicates Apple's shopping cart sidebar. It has a working demo and a very small code footprint.
why not use css position: fixed; property? of course if you don't mind the div being not smooth but straightly following your scrollTop. I've found it not working only in IE6-- by today, so using fixed position is a good solution I think, otherwise you just get with window.scrollTop of DOM and assign it to your element's absolute position top

javascript getelementbyid ... how to 'get' the "html" variable

function allowscroll()
{
if (screen.width<1200){document.getElementById('html').style.cssText='overflow-x: scroll !important;';};
}
<body onLoad="allowscroll();">
hi there, the above code works for any element, e.g. subbing "html" for "wrapper", but how is it possible to edit the css applied to html? Basically, because of overflow:hidden not inheriting in ie7 - which causes a big empty righthand margin and horizontal scrollbar (only in ie7, ie 8 compatibilty), ive set the css to
html {overflow-x:hidden;}
this is the only way to fix it without losing necessary functionality, e.g. overflowed graphics visibilty.
and this is all well and good, however, lower screen resolutions need the horizontal scroll just to see all of the page itself, so I'm attempting to restore the horizontal scrollbar for those users - and restore the big right margin for anyone who happens to be, for example ie7 1024 by 768 - I can live with that (unless anyone happens to have a superdupa solution).
document.getElementById('html').style.cssText='overflow-x: scroll !important;';
So the above code works for editing the CSS of any element, but not the CSS of the html.
I've also tried:
function allowscroll()
{
if (screen.width<1200){document.getElementByName('html').style.cssText='overflow-x: scroll !important;';};
}
and
function allowscroll()
{
if (screen.width<1200){window.style.cssText='overflow-x: scroll !important;';};
}
I would really appreciate any help, - if it helps in seeing the solution, the link where this applies is: delight design, basically, its how to take out:
html {overflow-x:hidden;}
from the css when in lower screen resolutions...
many thanks
Will
There are a bunch of different ways to get the html element:
document.documentElement
document.getElementsByTagName('html')[0]
document.body.parentNode
But in all honesty, there must be a better way. I don't have time right now to track down what exactly happened here, but from what I can tell, adding position:relative to whatever needs the overflow might help.
Try document.getElementsByTagName("html")[0]
Note I just edited the answer as getElementsByTagName returns an array. You want the first element in that array.
Just use the documentElement:
document.documentElement
It has full browser suport.

Categories