as you may already know I'm new to jQuery, so Code-Improvements not belonging to this theme are still very allowed.
This is my HTML-Code:
<div style="display: inline-block; width: 120px;">
<div>
Bananas:
<br />
<input id="bananas_amount" />
<input id="bananas_amount_percent" />
</div>
<br />
<div id="bananas" style="height:200px;"></div>
</div>
And this is my horrifying js-code:
$( "#bananas" ).slider({
orientation: "vertical",
range: "min",
min: 0,
max: 100,
value: 20,
step: 5,
slide: function( event, ui ) {
$( "#bananas_amount_percent" ).val( ui.value + " %" );
// the code displays a percentage by standart, but I need the real value, too:
var bananas_amount_percent = $( "#bananas_amount_percent" ).val();
var bananas_amount_percent = bananas_amount_percent.replace(" %", "");
var bananas_amount = Math.round((weight / 100) * bananas_amount_percent);
$( "#bananas_amount" ).val( bananas_amount + " g" );
}
});
$( "#bananas_amount_percent" ).val( $( "#bananas" ).slider( "value" ) + " %" );
// again the real value (else the value would not be updatet on reload-reset)
var bananas_amount_percent = $( "#bananas_amount_percent" ).val();
var bananas_amount_percent = bananas_amount_percent.replace(" %", "");
var bananas_amount = Math.round((weight / 100) * bananas_amount_percent);
$( "#bananas_amount" ).val( bananas_amount + " g" );
(weight is 200)
However, it's working, except one "little" detail: not with the mousewheel! I already found out that I need this extension: https://github.com/brandonaaron/jquery-mousewheel/downloads
But I've really absolutely no idea how to implement this to my Slider (I've 5 on my Site btw).
Help pls, Thx!
I haven't used the mousewheel plugin before but from what I've seen it should be used like this:
With DivName probably being your slider and I believe Delta is the scroll direction (in units) So I would guess negative is up, positive down.
The mousewheel plugin is too heavy for its role. I extracted the essence. Works great in all browsers.
EDIT: changed
#slider
to#bananas
EDIT2: added triggering
slide
eventBecause you are using only
value
property I pass for parameter object with only this property. If you will need something more, remember to add it to the mousewheel code.EDIT3: added change cancelling ability when
slide
function returnsfalse
(just like in the documentation)UPDATE:
delta
shows not only the wheel direction, but also has a deeper meaning. It describes number of pixels to scroll. The default setting for scrolling is 3 lines which is 120 pixels, but it can differ.If you want to retrieve only direction of wheel, change this:
to this:
although
delta === 0
should never occur.UPDATE: Added part for newer versions of jQuery (e.originalEvent).