Applying css3 gradients with jQuery

前端 未结 6 1810
-上瘾入骨i
-上瘾入骨i 2020-12-19 01:01

Eventually, I wish to dynamically alter gradients based on different things, but how do I get jquery to apply a css3 gradient?

 //works
 $(element).css(\"bac         


        
相关标签:
6条回答
  • 2020-12-19 01:17

    Here is a small piece of example...

    $("p").css({background:'linear-gradient(red,blue,red)'});
    
    0 讨论(0)
  • 2020-12-19 01:20

    Another optionis to use the jQuery addClass method. This allows you to dynamically add and remove classes and therefore any formatting associated with that class including gradients.

    0 讨论(0)
  • 2020-12-19 01:26

    When using .css() in jquery, I believe you have to use the shortened versions of the attributes. For example, margin-left would be marginLeft

    $(element).css("backgroundImage","-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255))");
    
    0 讨论(0)
  • 2020-12-19 01:28

    I'm using the hyphenated syntax in the JSON format (I always use the JSON format to be consistent). And it's working fine in both Chrome and Firefox.

    For example:

    $("#googleFeed div:even").css({
        'background':'-webkit-linear-gradient(top,white,black)',        
    });
    
    0 讨论(0)
  • 2020-12-19 01:37

    try this

    <div class="mygradient" />
    
    $( '.mygradient' ).css( 
        "background-image", 
        "linear-gradient( to right, #dc8a8a 50%, red 10% )" 
    );
    
    0 讨论(0)
  • 2020-12-19 01:40

    Your second approach looks OK... Maybe you need to css styles for non-webkit browsers as well... Cross-Browser CSS Gradient

    This works for me in Chrome

    $('#block').css({
        background: "-webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000))" 
    });
    

    Also have a look at: http://www.colorzilla.com/gradient-editor/

    0 讨论(0)
提交回复
热议问题