0

I have a server sending status-updates every 50ms.

I want to set the buttons on my page up to display the according state.

I had

if (data.playspeed == 100) {
    console.log("play");
    $('#prev').css('background-image','url(../public/images/skipr.png)');
    $('#next').css('background-image','url(../public/images/skipf.png)');
    $('#rwd').css('background-image','url(../public/images/rwdbutton.png)');
    $('#stop').css('background-image','url(../public/images/stopbutton.png)');
    $('#play').css('background-image','url(../public/images/playbutton-active.png)');
    $('#ffwd').css('background-image','url(../public/images/ffwdbutton.png)');
}

and it worked as intended. Problem: on an ipad, the buttons are blinking, because the images are refreshed too often. so i changed the code to:

if (data.playspeed == 100) {
    if ($('#play').css('background-image') != 'url(../public/images/playbutton-active.png)') {
        console.log("play");
        $('#prev').css('background-image','url(../public/images/skipr.png)');
        $('#next').css('background-image','url(../public/images/skipf.png)');
        $('#rwd').css('background-image','url(../public/images/rwdbutton.png)');
        $('#stop').css('background-image','url(../public/images/stopbutton.png)');
        $('#play').css('background-image','url(../public/images/playbutton-active.png)');
        $('#ffwd').css('background-image','url(../public/images/ffwdbutton.png)');
    }
}

my logic: only update the buttons to play-state, if the play-button is not yet set to be active.

but it doesn't work and i have NO idea why. the console.log still fires everytime (and the buttons still blink).

why?

3 Answers 3

2

I would suggest not using the style itself to check against as browser are prone to correcting tiny mistakes, meaning things aren't exactly as expected. Heres what I would suggest:

if (data.playspeed == 100) {
    if (!$('#play').hasClass("js-activated")) {
        $('#play').addClass("js-activated");

        console.log("play");
        $('#prev').css('background-image','url(../public/images/skipr.png)');
        $('#next').css('background-image','url(../public/images/skipf.png)');
        $('#rwd').css('background-image','url(../public/images/rwdbutton.png)');
        $('#stop').css('background-image','url(../public/images/stopbutton.png)');
        $('#play').css('background-image','url(../public/images/playbutton-active.png)');
        $('#ffwd').css('background-image','url(../public/images/ffwdbutton.png)');
    }
}

Add a class to your play button and check for that. However, when doing that it might be best to just move the entire background-declaration into an external CSS file instead of setting it with javascript.

What I'd suggest is adding these lines to your CSS:

#prev.js-activated { background-image: url(../public/images/skipr.png); }
#next.js-activated { background-image: url(../public/images/skipf.png); }
#rwd.js-activated  { background-image: url(../public/images/rwdbutton.png); }
#stop.js-activated { background-image: url(../public/images/stopbutton.png); }
#play.js-activated { background-image: url(../public/images/playbutton-active.png); }
#ffwd.js-activated { background-image: url(../public/images/ffwdbutton.png)');

(make sure you path is from your CSS file, though!) And replacing your JS with the following:

if (data.playspeed == 100 && !$('#play').hasClass("js-activated")) {
    $('#play, #next, #rwd, #stop, #prev, #ffwd').addClass("js-activated");
}

This makes both your JS and your CSS much more parseable and separated.

Sign up to request clarification or add additional context in comments.

7 Comments

problem is that i always have to remove the class from all others, but i think it's gonna work this way. actually it looks pretty easy this way, gonna try it
@PaulSchneider It might actually be a lot easier if you actually give all your elements a class to begin with (say class="button") and then use that in your scripts and CSS, much easier! Good luck though :)
how would that help? sry i might not grasp what you mean. i'd still have to check/change every specific button, right?
@PaulSchneider Well mostly to simplify your code. Use classes to group objects with similar properties. Say you want every one of those buttons to have a red background color, then you can define the rule .button { background-color: red; } in CSS. In jQuery, you can address all these buttons using $(".button") instead of naming ten IDs. In short, it simplifies and abstracts away your code a bit. Thats what classes are for, to group multiply objects with similar properties. My CSS above, for example, could be used to move your bg 100px to the left, revealing a hidden part of backrgound.
sure, but in the end i'm still gonna have to set each button a seperate bg-image, right? stopbutton.png for stopbutton, playbutton-active.png for playbutton and the same for other states (imagine fast forwarding), every state has to have a special button-representation for each button so in the end, i'll still have to address every button on its own
|
1

That behavior happen because the browser change the relative url to an absolute one.

In other, if the background image is ../test.png in the CSS, .css() will return http://www.example.com/parents/of/current/folder/test.png.

Just get the file name and compare it instead :

if ($('#play').css('background-image').split('/').pop() != 'playbutton-active.png)')
//The ending parenthesys is still there though...

But there is a problem, no browser work the same way. So maybe that condition will not work because some browser wrap the URL between quotes.

That being said, the best way to do what you wanna do is to use a flag. You could use a class as flag :

if (data.playspeed == 100) {
        if (!$('#play').hasClass('active')) {
            console.log("play");
            $('#prev').css('background-image','url(../public/images/skipr.png)');
            $('#next').css('background-image','url(../public/images/skipf.png)');
            $('#rwd').css('background-image','url(../public/images/rwdbutton.png)');
            $('#stop').css('background-image','url(../public/images/stopbutton.png)');
            $('#play').css('background-image','url(../public/images/playbutton-active.png)')
            .addClass('active');
            $('#ffwd').css('background-image','url(../public/images/ffwdbutton.png)');
        }
    }

That way, you are sure that no matter which browser you are, it will work.

Comments

0

Reduce to using background property or anyother css property in jquery whether use add class and check because the property created inline style in html and classes must be reusable and we use any where with class

 $('#prev').addClass('prev');

in css

.prev{
  background-image : 'url(../public/images/skipr.png)' ;
}

if you check with jquery using hasClass().it returns boolean

if (data.playspeed == 100) {   
  if($(#prev).hasClass(prev)){

    }
   // if condition here like that
}

4 Comments

Avoid using background property in jQuery. Why?
@FredericHamidi Not really a need to avoid it, but classes are faster. And easier to control.
because the property created inline style in html and classes must be reusable and we use any where
@JqueryKing Then you meant avoid setting CSS directly, not just backgrounds. Then the statement makes sense. There is no reason for background setting to be worse than, say, color setting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.