SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can only change opacity of image if style is initially specified inline (FF2.0)

    I'm trying to set an image to opaque on rollover, but haven't been able to make it to work in FF(2.0) unless I initially specify the opacity styles inline.

    In this example, the first image has inline styles and the rollover works.
    Styles for the second image are specified in the header and the rollover doesn't work.

    HTML Code:
      <head>
        <script type="text/javascript" language="javascript">
          function makevisible(cur,which){
            strength=(which==0)? 1 : 0.2  
            if (cur.style.MozOpacity){
              cur.style.MozOpacity=strength;
            }
            else if (cur.filters){
              cur.filters.alpha.opacity=strength*100;
            }
          }
        </script>
        <style type="text/css">
          img{
            width: 93px;
            height: 144px;
            margin: 20px;
            }
          #img2{
            filter: alpha(opacity=20);
            -moz-opacity: 0.2;
            }
        </style>
      </head>
      <body>
        <img id="img1" src="nohandsKitten.jpg" onMouseOver="makevisible(this,0)" onMouseOut="makevisible(this,1)" style="filter: alpha(opacity=20);-moz-opacity: 0.2;" />
        <img id="img2" src="nohandsKitten.jpg" onMouseOver="makevisible(this,0)" onMouseOut="makevisible(this,1)" />
      </body>
    From the English nation to a US location.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    The property check is giving you trouble.

    Code javascript:
    if (cur.style.MozOpacity){

    Right now it's performing a loosely-typed check on the property, one where undefined, null, and "" all equate to false.

    The second image has no style assigned to it, so it will be empty until something is assigned.

    See http://developer.mozilla.org/en/docs...ison_Operators for more information about the comparison operators.

    You should check to see that the property is not undefined instead.

    Code javascript:
    if (cur.style.MozOpacity !== undefined){
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome! Thanks Paul. I'd tried echoing the value back and when it returned null as opposed to "not defined" I didn't think anything of it.


    Thanks again for you help.

    Andy
    From the English nation to a US location.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •