简体   繁体   English

将鼠标悬停在一个div上,在另一个div中生成图像的不透明度(使用jquery)

[英]Hover over one div, effect the opacity of an image in another div (with jquery)

I'm working on this website: http://users.telenet.be/blijvendvertrek/default.htm . 我正在这个网站上工作: http//users.telenet.be/blijvendvertrek/default.htm

Now I want to establish that when I hover over one of three navigation items, ('Renovaties', 'Gerechtelijke expertise' or 'Over mij') located in a , the corresponding image in the right below it changes its opacity (from 0.6 to 1.0). 现在我想确定当我将鼠标悬停在位于a的三个导航项目之一('Renovaties','Gerechtelijke expert'或'Over mij')时,右下方的相应图像会改变其不透明度(从0.6到1.0)。

Via CSS, I have already established that each of those three images changes its opacity if you hover over it directly, but I can't seem to generate the same effect when I hover over the navigation items. 通过CSS,我已经确定如果你直接将鼠标悬停在它上面,这三个图像中的每一个都会改变其不透明度,但是当我将鼠标悬停在导航项目上时,我似乎无法生成相同的效果。 I've inserted a script to achieve this effect, but it doesn't work. 我已经插入了一个脚本来实现这种效果,但它不起作用。

This is the html for one of the three navigation items: 这是三个导航项之一的html:

<div id="navigation">
    <ul>
    <li id="1"><a href="renovaties/renovaties.htm" title="Renovaties">RENOVATIES</a></li>
    </ul>
    </div>

This is the html for the corresponding content item: 这是相应内容项的html:

<div id="content">
    <div class="kolom links">
       <a href="renovaties/renovaties.htm" title="Renovaties">
                        <img src="images/Icoon - Renovaties.png" alt="Renovaties" id="img-1"/></a>
    </div>
    </div>

And this is the script I can't get to work: 这是我无法工作的脚本:

$("#img-1, #img-2, #img-3").css('opacity','0.6');

$("#1").hover(function () {
    $('#img-1').css({opacity : 1.0});
  }, 
  function () {
    $('#img-1').css({opacity : 0.6});
  }
);

$("#2").hover(function () {
    $('#img-2').css({opacity : 1.0});
  }, 
  function () {
    $('#img-2').css({opacity : 0.6});
  }
);

$("#3").hover(function () {
    $('#img-3').css({opacity : 1.0});
  }, 
  function () {
    $('#img-3').css({opacity : 0.6});
  }
);

Any ideas on what I'm doing wrong? 关于我做错了什么的任何想法? Thank you all very much. 非常感谢你们。

DEMO: http://jsfiddle.net/Xc6ug/2/ 演示: http//jsfiddle.net/Xc6ug/2/

Better To use mouseenter and mouseleave instead of hover like this 更好使用mouseenter和mouseleave而不是像这样悬停

$("#1").mouseenter(function () {
$('#img-1').css({"opacity","1"});
});

$("#1").mouseleave(function () {
$('#img-1').css({"opacity", "0.6"});
});

id cannot be only numeric in html < 5 id不能只是html <5中的数字

as your li are in order, you can use .index() 因为你的李是有序的,你可以使用.index()

$("#navigation li").hover(function(){
    var n = $(this).index()+1;
    $('#img-'+n).css({opacity:1});
},function(){
    var n = $(this).index()+1;
    $('#img-'+n).css({opacity:0.6});
});

Where you do this code 你在哪里做这个代码

$("#img-1, #img-2, #img-3").css('opacity','0.6');

Try setting the opacity instead in a css file or as a inline style when the page loads. 尝试在css文件中设置不透明度,或在页面加载时设置为内联样式。 Just encase this code is continuously resetting any changes you make on the fly. 只需加入此代码就可以不断重置您在运行中所做的任何更改。

Just an idea. 只是一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM