最後に、DOM内のホバー要素の上にあるsvgのホバーカラーを変更するコードが見つかりましたが、ここでロジックを追加したいと思います。この時点で.on()関数の中にif/else文を置くことはできますか?
HTML
<div class="tag--active grid__item small--one-third medium--one-third large--one-third xlarge--one-third">
<div class="tag-container">
<a class="svg-filter-link" href="">
<div class="top clearfix">
<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->
<svg width="41px" height="48px" viewBox="0 0 41 48" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 39.1 (31720) - http://www.bohemiancoding.com/sketch -->
<title>filter-icn-bedbug</title>
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Pest-Peeve-Web" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Shopfront" transform="translate(-402.000000, -706.000000)" fill="#C8C8C8">
<g id="filter-icn-pest" transform="translate(154.000000, 698.000000)">
<path d="M284.00492,...bunch of numbers... L277.822737,10.9244995 Z" id="filter-icn-bedbug"></path>
</g>
</g>
</g>
</svg>
</div>
</a>
<div class="bottom">
<a title="Title" href="/collections/tag">Link Text</a>
</div>
</div>
</div>
はJavaScript
$(document).on({
mouseenter: function() {
$(this).closest('.tag-container').find('svg path').css({'fill': '#0BACFF'});
},
mouseleave: function() {
$(this).closest('.tag-container').find('svg path').css({'fill': '#939393'});
}
}, ".tag-container a");
ホバリング時に推移していないとき、それは、その後、戻って変更する必要がありますように、SVGは色が変わります。しかし、問題は、ページが読み込まれたときに「タグアクティブ」アイコン(アクティブリンク)がすでに強調表示されているため、その上にマウスを置くと強調表示が削除されることです。
私は「それはすでにこの色なら、何もしない」、と言ってロジックを追加したいのですが、以下は動作しません。
$(document).on({
if ($(this).closest(".grid__item").hasClass("tag--active") == 'false') {
mouseenter: function() {
$(this).closest('.tag-container').find('svg path').css({'fill': '#0BACFF'});
},
mouseleave: function() {
$(this).closest('.tag-container').find('svg path').css({'fill': '#939393'});
}
}
}, ".tag-container a");
私は私が間違っている構文を使用している必要がありますと思います。どうしますか?
あなたは '$(this)'からどんなコンテキストを取得しているかを確認します。 'console.log()'に出力してみてください – bbodien
イベントとそのハンドラを表す2つのプロパティを持つオブジェクトを 'on'に渡しています。それらは2つの別々のイベントであるため、 'if'は各メソッドの内部で実行する必要があります。 'if'文や' while/for'ループやオブジェクトリテラルの中に他のものを入れることはできません。これは構文エラーにつながります。 – ste2425
ああ、明確化のおかげで。私は今、より良い理解を持っています。 – Kevmon