2011-10-26 5 views
2

아래 스크립트는 완벽하게 작동합니다. 그러나, 그것은 내가 성취하려고하는 것이 아닙니다. 마우스를 올리면 1 개의 색상을 선택하고 마우스를 올리면 링크 당 모든 색상을 순환하는 대신에 그 위에 머무르고 싶습니다. 당신은 효과의 예 난 당신이 .data()와 요소에 색상을 표시 할 수 있습니다 여기 http://www.morxmedia.com/jQuery 마우스 오버시 랜덤 링크 색상

$(document).ready(function() { 
    original = $('.menu-item a').css('color'); 
    $('.menu-item a').hover(function() { //mouseover 
    var rand = Math.floor(Math.random() * 4); 
if(rand == 0){var col = '#EAD325';} 
else if(rand == 1){var col = '#FE902F';} 
else if(rand == 2){var col = '#82BE38';} 
else if(rand == 3){var col = '#217AFC';} 
else{var col = '#888888';} 

    $(this).animate({'color': col,}); 
    },function() { //mouseout 
    $(this).animate({ 
     'color': original, 
    }); 
    }); 
}); 

답변

2

각 링크마다 임의의 색상을 생성하여 배열에 저장합니다. 마우스를 올리면 색상을 확인하고 애니메이션을 적용합니다. 나는 자바 스크립트와 매우 효율적인 아니에요 http://jsfiddle.net/fRqj2/

$(document).ready(function() { 

    var assignedColors = new Array(); 

    $('.menu-item a').each(function(i) { 
     var rand = Math.floor(Math.random() * 4); 
     if (rand == 0) { 
      var col = '#EAD325'; 
     } 
     else if (rand == 1) { 
      var col = '#FE902F'; 
     } 
     else if (rand == 2) { 
      var col = '#82BE38'; 
     } 
     else if (rand == 3) { 
      var col = '#217AFC'; 
     } 
     else { 
      var col = '#888888'; 
     } 
     assignedColors[i] = col; 
    }); 

    original = $('.menu-item a').css('color'); 
    $('.menu-item a').hover(function() { //mouseover 
     index = $(this).parent().prevAll().length; 
     $(this).animate({ 
      'color': assignedColors[index] 
     }); 
    }, function() { //mouseout 
     $(this).animate({ 
      'color': original, 
     }); 
    }); 
}); 
+0

고마워요! 그러나 jsfiddle에서 색상을 무작위로 추출하는 것은 아닙니다. – Stephen

+0

그것은 여기에서 작동합니다. 페이지를 새로 고침 할 때 색상 만 업데이트해야한다는 데 동의합니까? 이것을 시도하십시오 : http://jsfiddle.net/fRqj2/1/ - 경고에 임의의 색상을 게시해야합니다. updaing :) –

+0

흠, 음 그냥 테스트 해봤는데, 아마도이 코드는 내가 사용했던 다른 코드와 함께 생겼습니다. .. 무작위적인 색상을 가지고있는 것처럼 보입니다. 여기에서 볼 수있는 다른 임의의 색상으로 변합니다. http://morxmedia.com/ 코드를 사용하고 있습니다. – Stephen

0

을 원하지 않는 볼 수 있습니다. 다음과 같은 내용 :

... 
    col = $(this).data("hoverColor"); // Tries to load the color for the element 
    if (! col) { 
    col = getRandomColor(); 
    $(this).data("hoverColor", col); // Saves the color for the element 
    } 
    ... 
+0

:

작동 예를 참조하십시오. 나는 html/css와 그래픽 디자인 haha에 최상이다. 목록에있는 색상으로 복사하여 붙여 넣을 수있는 방식으로 다시 쓸 수 있습니까? 나는 정말로 감사 할 것입니다. :) – Stephen

관련 문제