4

次のプラグインを使用しているラバランプ メニューのテキストの色を変更しようとしていますhttp://www.queness.com/post/530/simple-lava-lamp-menu-tutorial-with-jquery

私がやったことは次のとおりです

 $('#lava').mouseleave(function () {

    $('#lava li').removeClass('selected');  
     $('#lava li').css({color: '#FFF'});  
    //select the current item
    $(this).addClass('selected');  
    $(this).css("color", "white");     

});

ただし、マウスを離すと、すべてのテキストが正しい黒に変わりますが、$(this) は白に変わりません

ここにコードと動作デモのコピーがあります

http://jsfiddle.net/aSr3J/

4

3 に答える 3

1

あなたが求めているのはこれだと思います:

http://jsfiddle.net/aSr3J/20/

基本的に、mouseleave 関数は次のようにする必要があります。

$('#lava').mouseleave(function () {

    left = Math.round($(".selected").offset().left - $('#lava').offset().left);
    width = $(".selected").width();

    //Set the floating bar position, width and transition
    $('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});   
    $('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});      

});

スタイル シートにリンクの色の定義も追加したことに注意してください。

#lava ul a li {  color:#fff; }

li(インライン要素のようにブロックレベルの要素を囲むことaは、HTML5 でのみ有効であることをご存知ですか?)

メニューテキストの色については、以下も修正しました $('#lava li').hover(function ())

   $('#lava li').hover(function () {

    //Get the position and width of the menu item
    left = Math.round($(this).offset().left - $('#lava').offset().left);
    width = $(this).width();
    $(this).css("color","black");

    //Set the floating bar position, width and transition
    $('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});   
    $('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});    

//if user click on the menu
},function() { $(this).css("color","white");}).click(function () {

    //reset the selected item
    $('#lava li').removeClass('selected');  

    //select the current item
    $(this).addClass('selected');

});
于 2012-01-19T00:44:01.427 に答える
0

コードが正しくないことはほぼ確実です。彼らのキーワード「this」は、他の言語に慣れているプログラマーにとって非常に驚くべき方法で変化する魔法の獣です。

最初にこれを読んで、「これ」とは何か、どのように変更されるかを理解してください。

http://howtonode.org/what-is-this

次に、jquery 関数プロキシ (http://api.jquery.com/jQuery.proxy/) を使用して、「this」を関数にカプセル化します。

$('#lava').mouseleave($.proxy(function () {
    $('#lava li').removeClass('selected');  
    $('#lava li').css({color: '#FFF'});  
    //select the current item
    $(this).addClass('selected');  
    $(this).css("color", "white");     

}, this));
于 2012-01-19T00:01:01.827 に答える
0

各liのホバーで色を変えてみてください

// the follow preforms for loop on your li's
$("#lava li").each(function(i) {
        // this is the "hover" function, aka, mouseenter and mouseleave
    $(this).hover(function(eIn) { // this function allows you to do stuff while mouse is over object
        $(this).addClass('selected').css("color", "#FFF"); // FFF is white
    },
    function(eOut) { // this allows you to do stuff when mouse leaves object
        $(this).removeClass('selected').css("color", "#000"); // 000 is black
    });
});
于 2012-01-19T00:01:10.217 に答える