Text over image using jquery and css

2019-07-04 23:18发布

问题:

Im using the tutorial http://www.webdesignlondon-tristar.co.uk/website-design-london/insane-jquery-image-rollover.

I am wanting a fading/darken of the image on rollover and for the text to appear as in the tutorial. However for some reason I cannot seem to get it to work even when starting a blank document.

This is not the first time I have used jquery so Im confused why I can't get it working.

I have used all the code together;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style type="text/css">
.wrapper            { padding: 8px; background-color: #fff; border: 1px #0d5a2c solid; position: relative; width:300; height: 300; margin: 0 auto; }
.wrapper:hover      { cursor: pointer; }
.description        { display: none; background-color: #000; color: #000; position: absolute; left: 8px; top: 8px; text-decoration: none; font-size: 1.5em; width: 250px; height: 130px; padding: 120px 0 0 0; }
.description img    { vertical-align: middle; margin: 0 2px 1px 0; }

</style>

<script src="jquery-1.3.1.min.js" type="text/javascript">
$(document).ready(function(){
    $('.wrapper').hover(function(){
        $(this).children('.description').stop().fadeTo(200, 0.8);
    },function(){
        $(this).children('.description').stop().fadeTo(200, 0);
    });
});
</script>

</head>



<body>
<div class="wrapper">
<img src="http://www.compuhex.co.uk/Version2/products2/blue.PNG" alt="Product 1" />
<a href="#" title="Click for More" class="description">
Want to read more?
</a>
</div>

</body>
</html>

the live link can be seen on http://www.compuhex.co.uk/Version2/hovertext/

sorry if the code is messy.

Thanks for all the help guys

so to sum up Im looking to see why the code isnt working even though it is a copy from the tutorial site. The technique shown is the EXACT thing I want to do to my website. Thanks

回答1:

Your using an old version of jQuery (1.3.1) it will work (as it standS) as expected with a version > 1.4;

http://jsfiddle.net/dwrUj/

In you example your wrapping your ready code in the <script src=.. element, you need to separate the two, i.e from:

<script src="jquery-1.7.2.min.js" type="text/javascript">
    $(document).ready(function(){
    ...
</script>

to:

<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script type='text/javascript'>
    $(document).ready(function(){
    ...
</script>


回答2:

Replace ur code as

<script src="jquery-1.3.1.min.js" type="text/javascript"></script>


<script type="text/javascript">

$(document).ready(function(){  

 $('.wrapper').hover(function(){


 $(this).children('.description').stop().show().fadeTo(200, 0.8);
},function(){
    $(this).children('.description').stop().hide().fadeTo(200, 0);
});


  });
</script>

It is working fine..Version is not a problem