jQuery Animate - border color and width

前端 未结 5 1783
忘掉有多难
忘掉有多难 2020-12-16 20:16

I cannot seem to get this jQuery animation working for applying a border to an image on mouseenter:

相关标签:
5条回答
  • 2020-12-16 20:49

    Fixed code:

    http://jsfiddle.net/9qwmX/491/

    $('div img').mouseenter(function () {
        $(this).css({
            outline: "0px solid transparent"
        }).animate({
            outlineWidth: '4px',
             outlineColor: '#f37736'
        }, 500);
    }).mouseleave(function () {
        $(this).animate({
             outlineWidth: '0px',
             outlineColor: '#037736'
        }, 500);
    });
    
    0 讨论(0)
  • 2020-12-16 20:58

    You have some typos in your code

    1. .mousenter should be .mouseenter

    2. didnt close the apostrophe in both 'borderColor. change them to 'borderColor'

    $('div img').mouseenter(function(){
        $(this).css("border", "0px solid #f37736").animate({
            'borderWidth': '4px',
            'borderColor': '#f37736'
        },500);
    }).mouseleave(function(){
         $(this).animate({
            'borderWidth':'0px',
            'borderColor':'#f37736'
        },500);
    });
    
    0 讨论(0)
  • 2020-12-16 20:59

    $.animate() works only on CSS properties that have single numeric values. Thus, you only need to specify the border's width, as the border-color property is ignored by $.animate().

    Other than that, the event is mouseenter, not mousenter.

    Here is the fixed code:

    $('div img').mouseenter(function () {
        $(this).css({border: '0 solid #f37736'}).animate({
            borderWidth: 4
        }, 500);
    }).mouseleave(function () {
         $(this).animate({
            borderWidth: 0
        }, 500);
    });
    

    Demo

    0 讨论(0)
  • 2020-12-16 21:01

    Change your jQUERY to this

    $('div img').mouseenter(function(){
        $(this).css("border", "0px solid #f37736").animate({
            'borderWidth': '4px',
            'borderColor': '#f37736'
        },500);
    }).mouseleave(function(){
         $(this).animate({
            'borderWidth':'0px',
            'borderColor':'#f37736'
        },500);
    });
    
    0 讨论(0)
  • 2020-12-16 21:06

    jQuery cannot animate color but its own, you need to include a seperate jQuery plugin for that.

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