【问题标题】:Change opacity css with jquery selector使用 jquery 选择器更改不透明度 css
【发布时间】:2015-02-19 03:47:45
【问题描述】:

我正在构建具有图标视图并相互编辑的品牌缩略图。当其中一个图标悬停时,我想悬停所有图标。

这是我的结构..

<div id="brands-wrapper">
                    <img class="brands" src="http://localhost/infodiskon/images/'.$data->image_brand.'">
                    <div id="icon-wrapper">
                        <a href="#" id="view">
                            <img src="http://localhost/infodiskon/assets/images/view_icon.png">
                        </a>
                        <a href="#" id="edit">
                            <img src="http://localhost/infodiskon/assets/images/edit_icon.png">
                        </a>
                    </div>
                    <h3>'.$data->brand_name.'</h3>
                    <h4>'.$data->location.'</h4>
                </div>

我使用 2.1.3 版的 jquery 和 google CDN。

而且我只是想在图标视图悬停时发出警报。一个脚本:

$(document).ready(function() {
        $("#view").hover(
            function() {
                alert("Yo");
            }
        );
    });

警报只是在第一个项目只悬停在另一个项目时才出现..

我想要的是在其中一个悬停时显示所有图标???我只想将不透明度更改为 1。我可以在 css 中使用 :hover 但它只使用它的标签。

css

#icon-wrapper{
    position: absolute;
    top: 0;left: 0;
}
#icon-wrapper .view, #icon-wrapper .edit{ 
    display: block;
    width: 194px;
    height: 94px;
    line-height: 94px;
    text-align: center;
    background-color: #363636;
    opacity: 0;
    color: white;
    text-decoration: none;
}
.view>img{margin-bottom: -16px;} 
.edit>img{margin-bottom: -10px;}
#icon-wrapper .view{margin-bottom: 6px;}
#icon-wrapper .view:hover, #icon-wrapper .edit:hover{
    opacity: .8;
}

【问题讨论】:

  • 您需要将.hover 应用于所有元素共享的类。目前,您只是将其绑定到一个名为 #view 的 id

标签: jquery css opacity


【解决方案1】:

您需要将类附加到您需要悬停的所有元素。您仍然可以保留您的 ID,因为您可能需要它用于其他功能。

HTML:

<div id="icon-wrapper">
    <a href="#" class="thumb" id="view">
        <img src="http://localhost/infodiskon/assets/images/view_icon.png">
    </a>
    <a href="#" class="thumb" id="edit">
        <img src="http://localhost/infodiskon/assets/images/edit_icon.png">
    </a>
</div>

JS:

$(".thumb").hover(function() {
    $(this).parent().children().css("opacity", "0");//when mouseenter
}, function() {
    $(this).parent().children().css("opacity", "1");//when mouseleave
});

【讨论】:

  • okey 明白了.. 当类视图悬停时我如何选择类编辑。我只想用 jquery 改变它的不透明度
  • @CodeGodie 你能解释一下我的回答被否决了吗?
  • 如果类不同怎么办??就像我上面的结构.. 类视图和编辑。他们在 css 中有不同的风格
  • 请在您的问题中提供您的 CSS,以便我们查看您目前拥有的内容。元素上的类不应该不同,你的 ID 应该不同,你的 CSS 应该指向这些 ID
  • css 已更新.. postimg.org/image/609qaft6f/643d2ea9 。这是我想要实现的。我有带有 2 个图标的品牌标志。查看和编辑。我在 css 中使用了悬停,但它只会影响它本身的元素。所以我想在一个图标悬停时悬停所有图标.. 我该怎么做??
【解决方案2】:

看起来您有多个具有相同 ID 的元素。 ID 必须是唯一的。你可以改用同一个类。

标记:

               <div class="icon-wrapper">
                    <a href="#" class="view">
                        <img src="http://localhost/infodiskon/assets/images/view_icon.png">
                    </a>
                    <a href="#" class="edit">
                        <img src="http://localhost/infodiskon/assets/images/edit_icon.png">
                    </a>
                </div>

脚本:

 $(".view").hover(
    function() {
       $(this).next().addClass('smclass');
    },function(){
       $(this).next().removeClass('smclass');
    }
);

Demo

【讨论】:

  • 您需要将该类添加到您的所有a 元素中
  • 不,它的工作,Fiddle here,一点 css 并改变了第二个锚的类
  • 好吧,我不知道为什么它不起作用..但我只想知道当类视图悬停时如何选择类编辑并更改它的css
  • 使用$(this).next().addClass('smclass')。查看更新的演示。
  • @MilindAnantwar 你能指出OP的代码中有“具有相同ID的多个元素”吗?
【解决方案3】:

您不需要将类附加到anchor 标记或img 标记。您可以改用CSS 中的direct children 选择器。这将为您节省每次添加新锚元素时附加类的负担。

注意:我刚刚编辑了img 标签的来源(并添加了一些CSS)以显示图像。

鉴于您的 HTML 格式为:

<div id="brands-wrapper">
    <img class="brands" src="http://localhost/infodiskon/images/'.$data->image_brand.'" />
        <div id="icon-wrapper">
            <a href="#" id="view">
                <img src="http://www.skrenta.com/images/stackoverflow.jpg" />
            </a>
            <a href="#" id="edit">
                <img src="http://www.skrenta.com/images/stackoverflow.jpg" />
                </a>
        </div>
     <h3>'.$data->brand_name.'</h3>

     <h4>'.$data->location.'</h4>

</div>

您可以做的是使用jQuery 来定位具有id“icon-wrapper”的div 的所有直接子代。

$(document).ready(function () {
    $("#icon-wrapper > a").hover(function () {
        alert("Yo");
    });
});

这将帮助您定位所有作为 div 中直接子级的锚标记。

$(document).ready(function () {
    $("#icon-wrapper > a").hover(function () {
        alert("Yo");
    });
});
#icon-wrapper > a > img {
    border: 2px solid Black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="brands-wrapper">
    <img class="brands" src="http://localhost/infodiskon/images/'.$data->image_brand.'" />
        <div id="icon-wrapper">
            <a href="#" id="view">
                <img src="http://www.skrenta.com/images/stackoverflow.jpg" />
            </a>
            <a href="#" id="edit">
                <img src="http://www.skrenta.com/images/stackoverflow.jpg" />
                </a>
        </div>
     <h3>'.$data->brand_name.'</h3>

     <h4>'.$data->location.'</h4>

</div>

【讨论】:

  • 啊,我为投反对票道歉。我以为你说的是​​完全不同的东西。你的回答会起作用,这只是调用选择器的另一种方式。
  • css 已更新.. postimg.org/image/609qaft6f/643d2ea9 。这是我想要实现的。我有带有 2 个图标的品牌标志。查看和编辑。我在 css 中使用了悬停,但它只会影响它本身的元素。所以我想在一个图标悬停时悬停所有图标.. 我该怎么做??
猜你喜欢
  • 2010-12-05
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 2013-02-05
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
相关资源
最近更新 更多