0

How can I remove mouse interaction over the slider but keep the function where it decreases when I click the button and change color? (only in jquery mobile and jquery)

So if I press on the slider nothing should happen, only when I press the button.

Can also the slider decrease in the opposite direction? From left to right?

This is the code:

HTML:

<div id='slider' class='sliderBar'></div>
<button>Remove 10%</button>

CSS:

html, body {
    height:100%;
    width:100%;
    margin:0;
    padding:0;
}
#slider {
    height:20px;
    max-height:20px;
}

.sliderBar-progress {
        background:rgb(0, 255, 0);
        transition-duration: 0.5s;
        -webkit-transition-duration: 0.5s; /* for Safari */;
    }

JS:

$('#slider').sliderBar({
    start: 100,
    onChange: function (val) {
        var red = 0,
            green = 0;
        if (val >= 50) {
            red = 255 - Math.round(((val - 50) / 50) * 255);
            green = 255;
        } else {
            red = 255;
            green = Math.round(((val) / 50) * 255);

        }
        $('.sliderBar-progress').css({
            background: "rgb(" + red + "," + green + ",0)"
        });
    }
});

$('button').on('click', function () {
 $('#slider').setsliderBar($('#slider').getsliderBar()-10, true);
});

Thank you!

Alex Stanese
  • 735
  • 4
  • 16
  • 33

1 Answers1

2

What you are really looking for is a progress bar, not a slider. In your example you appear to be using a third party plugin for the slider; so eliminating mouse interaction and getting right to left functionality would involve changing the plugin code.

Alternatively, it is pretty easy to roll your own with just the functionality you need.

Here is a DEMO

For markup you just need 2 divs:

<div id="slider" class="progressBar">
    <div class="progress"></div>
</div>

Then 2 CSS rules to make the track and bar look the way you want (tweak to your taste). Setting the position of the progress div to right: 0 will make it go from left to right.

.progressBar{
    position: relative;
    box-sizing: border-box;
    box-shadow: 1px 1px 3px #777 inset  ;
    -webkit-box-shadow: 1px 1px 6px #777 inset;
    height: 24px;
    max-height: 24px;
    overflow: hidden;
    width: 100%;
    border: 1px solid rgb(193, 193, 193);
    background-color: rgb(224, 224, 224);
    border-radius: 6px;
}

.progress {
    position: absolute;
    box-sizing: border-box;
    box-shadow: inset 0px 0 5px 0 #777;
    -webkit-box-shadow: inset 0px 0 5px 0 #777;
    right: 0px;
    height: 24px;
    margin-top: 0px;
    background-color: orange; 
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;   
    transition-duration: 0.5s;
    -webkit-transition-duration: 0.5s; /* for Safari */;
}

For getting and setting the progress/color, add these functions:

function GetProgress(){
    var val = $("#slider .progress").outerWidth();
    var tot = $("#slider").outerWidth();
    if (tot <= 0) return 0;
    return Math.floor(val * 100 / tot);
}
function SetProgress(val){
    if (val < 0) val = 0;
    if (val > 100) vall = 100;
    var color = GetColorForVal(val);
    $("#slider .progress").css({"background": color, "width": val + "%"});
}
function GetColorForVal(val){
    var red = 0,
        green = 0;
    if (val >= 50) {
        red = 255 - Math.round(((val - 50) / 50) * 255);
        green = 255;
    } else {
        red = 255;
        green = Math.round(((val) / 50) * 255);
    }
    return  "rgb(" + red + "," + green + ",0)";
}

You can then get and set progress by calling these functions:

$('#subtract').on('click', function () {
    var curVal = GetProgress() - 10;
    SetProgress(curVal);
});

UPDATE: There can be rounding errors calculating current width, so instead we can read it from the style:

function GetProgress(){
    var curWid = $("#slider .progress")[0].style.width || 100;
    return parseInt(curWid);
}

Updated DEMO

ezanker
  • 24,628
  • 1
  • 20
  • 35
  • Thank You! I also tried to implement this in the progressbar as you did but the problem is that it doesn't decreases with exactly 10%, for example the last red part is not 10%... I also encountred this problem... Is there a way to solve it within the progressbar implementation? – Alex Stanese Feb 27 '14 at 19:06
  • See my updated answer and demo. It should take care of the rounding issues. – ezanker Feb 27 '14 at 19:21
  • Is it possible that the progress should fade in and out if its is under 20%? I tried this code but it's not affcting the progress in any way: if (val < 20) pulsate('.progress'); function pulsate(element) { $(element || this).animate({ opacity: 0 }, 500, function() { $(this).animate({ opacity: 1 }, 500, pulsate); }); } – Alex Stanese Mar 03 '14 at 21:26
  • but if I try somethig else (ex: pulsate('.other_elem')) it works... wat is blocking the progress to be pulsating? – Alex Stanese Mar 03 '14 at 21:27
  • The transition duration of 0.5seconds was applying to opacity too. I have updated the fiddle so css animation only applies to width change: http://jsfiddle.net/ezanker/D4UqU/10/ (transition: width 0.5s;) and added you pulsate function. it now pulses below 20% – ezanker Mar 03 '14 at 22:04
  • now with the last update, the color transition isn't smooth anymore as before, it changes to the other color instantly not smooth... omg... there are alot of tricky issues with this... – Alex Stanese Mar 06 '14 at 15:46
  • I added -webkit-transition: background 0.5s; transition: background 0.5s; and now its working as it should! – Alex Stanese Mar 06 '14 at 17:12
  • Sounds like you got it, but here is an updated fiddle: http://jsfiddle.net/ezanker/D4UqU/12/ – ezanker Mar 06 '14 at 18:10
  • hey man... I think there's a curse around here... it is finally working as it should on fiddle or on ripple emulator... but when I test it on my device it has several bugs.. I described the situation in this post... maby you'll take a look if you are fammiliar with these problems... http://stackoverflow.com/questions/22235583/pulsating-effect-possible-on-phonegap-3-3 I also have my repo there :) – Alex Stanese Mar 06 '14 at 21:48
  • You could remove the CSS animation and strictly use jQuery animate (http://jsfiddle.net/ezanker/D4UqU/13/). Note I had to add a color animation plugin: http://www.bitstorm.org/jquery/color-animation/ – ezanker Mar 07 '14 at 14:43
  • it have the same effect as before... when I press the remove button on 20 % it moves to 10% and in that animation time it only fades out and in once and it stays there at 10% without pulsating... If I press again it goes almost comlpetely transparent and it shrinkes to 0%... But on browser or in ripple emulator it works allright only on the phone... its like the effect happends but very fast or in contradiction to a simmilar effect... But I tryed removing all the content from that page and still no luck... If you have time can you take a short look on my repo? it is on this post.. Yhank you! – Alex Stanese Mar 07 '14 at 16:36
  • It's a problem with the pulsating effect because I tried to add the pulsating effect to all the elemsts on the page: the progress bar, the frame div, to the 3 buttons and to the slider and the effect was very interesting: The frame, the progressBar and 2 of those 3 buttons were not really puslating, just flickering. The other button wasn't doing anything, not even flickering, and the slider from the bottom was pulsating correctly. Why aren't other divs pulsating? Has it something with the position (fixed, absolute, relative)?? Is there a way to fix this or it's a bug?? – Alex Stanese Mar 08 '14 at 10:33
  • And all this happend on the phone, on fiddle and on ripple emulator it works perfectly... Why is that? – Alex Stanese Mar 08 '14 at 11:06
  • I also tried it on galaxy s2 mini and the same effect. But on i9100 eclipse emulator it works fine.. What could it be? – Alex Stanese Mar 08 '14 at 17:49
  • so... can you please give me a tip? :) I'm stuggeling since weeks to get the thing working... – Alex Stanese Mar 09 '14 at 10:52
  • Please can you take a short look at my post? http://stackoverflow.com/questions/22235583/simple-pulsating-effect-with-jquerry-on-phonegap-3-3 I also set a bounty if you can help me pass this issue :D – Alex Stanese Mar 10 '14 at 09:11
  • but are there other alternatives to achive my desired effect? Maby its a bug with the opacity or something like that... – Alex Stanese Mar 10 '14 at 19:19
  • this has absolutely no logic! I solved it with this magic line: -webkit-transform: translateZ(0); – Alex Stanese Mar 17 '14 at 11:01
  • Thanks for letting me know. You are right, that makes no sense at all! – ezanker Mar 17 '14 at 14:21
  • this function also forces the device to use the graphic processor and the transition happends more smooth! – Alex Stanese Mar 18 '14 at 13:46
  • http://davidwalsh.name/translate3d - Yes, translate3d forces graphics accelaeration in webkit browsers... – ezanker Mar 18 '14 at 14:07