jQuery class selector doesn't work and id selector works only with “body”

前端 未结 5 733
生来不讨喜
生来不讨喜 2021-01-21 17:32

I\'m trying to apply CSS values with jquery but class selector or id selector does not not work for some reason.

Here is my fiddle:

As you can see nothing happen

5条回答
  •  轮回少年
    2021-01-21 18:06

    First you need do:

    $("#kitten").css("background-position", x + "px 0");
    

    Becouse you are not getting the Id selector.

    And define a width and height to your div, to show your image like:

    #kitten {
        background-image: url("http://sereedmedia.com/srmwp/wp-content/uploads/kitten.jpg");
        background-repeat: repeat-x;
        width: 390px; <----- LIKE this
        height: 200px; <----- LIKE this
    }
    

    DEMO

提交回复
热议问题