【问题标题】:Hover over div changes another div heading将鼠标悬停在 div 上会更改另一个 div 标题
【发布时间】:2015-04-30 03:03:02
【问题描述】:

我会尽力解释这一点。 我有 5 个盒子,希望 1 个盒子有不同的标题颜色。当我将鼠标悬停在其他 4 个框上时,我希望更改活动框标题,从而使新标题变为不同颜色。鼠标关闭,标题将保持该颜色。

我进行了设置,因此当您将鼠标悬停在图像上时,段落文本会根据图像而变化。当悬停动作发生时,我有占位符文本消失。

我尝试的一切都不起作用,但我知道有一个答案。太感谢了!

这是我的代码:

<div class="process">     
            <div class="step box-1">
                <img src="img/seed.jpg" rel="first-step">
                <h3>SEED</h3>
            </div>
            <div class="step box-2">
                <img src="img/seedling.jpg" rel="second-step">
                <h3>CULTIVATE</h3>
            </div>
            <div class="step box-3">
                <img src="img/drop" rel="third-step">
                <h3>DISTILL</h3>
            </div>
<h4 class="steps first-step"> 
            Powerful, effective essential oils come from seeds and plants that are verified for their essential oil potential by Young Living experts, partnering with university experts. 
            </h4>
            <h4 class="steps second-step">
            Young Living farms, located around the globe, are dedicated to perfecting the best growing and harvesting methods. Our experts also travel the world visiting our co-op farms to verify that their growing and cultivating processes match our high standards. These operations provide an ongoing source for essential oils that meet Young Living's demanding quality standards. 
            </h4>
            <h4 class="steps third-step">
            Combining ancient and modern techniques, Young Living is recognized as an innovator in essential oil distillation. We use a gentle, proprietary technique for steam extracting the most effective essential oils, as well as using cold pressing and resin tapping methods for select oils. 
            </h4> 
            <div>

JS:

$(document).ready(function () {
var $placeholder = $('.placeholder');
$('.step> img').hover(function() {
var $rel = $(this).attr('rel');
$placeholder.hide();
});
});

$(document).ready(function () {
var $captions = $('.steps');
$captions.hide();
$('.step> img').hover(function() {
var $rel = $(this).attr('rel');
$captions.hide();
$('.steps.'+ $rel +'').show();
});
});

【问题讨论】:

  • 哪个盒子应该有不同的颜色?你能举个例子吗?
  • 所以只有选定的标题应该是不同的颜色?
  • 当用户访问页面时,“种子”的占位符文本应该是不同的颜色。当您将鼠标悬停在其他图像上时,文本会发生变化,标题颜色也会发生变化。我知道如何进行简单的悬停,但我需要“种子”颜色才能恢复到原始颜色。
  • 当用户选择一个项目时,该项目的样式应该改变,而其他人保持默认样式。这对每个选定的项目重复吗?
  • 是的,并且在悬停后总是选择一个项目

标签: jquery css mouseover jquery-hover


【解决方案1】:

试试这个代码。

$(document).ready(function () {
    var $placeholder = $('.placeholder');
    var $captions = $('.steps');
    $captions.hide();
    $('.step> img').hover(function() {
        var $rel = $(this).attr('rel');
        $captions.hide();
        $placeholder.hide();
        $('.step h3').css('color', 'black');
        $(this).siblings('h3').css('color', 'red');
        $('.steps.'+ $rel +'').show();
    });
});

首先将所有.step h3 设置为颜色黑色。然后将活动的h3 设置为红色

【讨论】:

  • 这成功了!太感谢了。我不会想出兄弟选择器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-30
  • 2013-04-01
  • 2023-03-13
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多