【问题标题】:Changing image size during mouse hover在鼠标悬停期间更改图像大小
【发布时间】:2014-07-27 17:36:21
【问题描述】:

我目前正在尝试这样做,以便如果我将鼠标悬停在li 上,图像会改变其大小。如果我将鼠标悬停在img 上,我设法从一个网站中提取了一个代码,该代码如下所示:

<div class="profiles">
    <ul>
        <li class="portraitsLeft" id="one" align="left">
            <a href="../project/profile1.html">
                <img src="../project/images/portraits/img1.jpg" width="100" onmouseover="this.width=150;" onmouseout="this.width=100" align="middle" alt="Img1"/>
            </a> 
            Character 1
        </li>
    </ul>
</div>

我不明白this 指的是什么。我假设它指向img,但它似乎不起作用。当我将鼠标悬停在lia 而不是this 上时,我应该如何更改代码以便更改图像大小?提前致谢。

【问题讨论】:

标签: html


【解决方案1】:

在这种情况下不需要 CSS(尽管它更简洁)。您可以使用name 属性来执行此操作。

<img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/OrteliusWorldMap.jpeg" width="150" height="100" name="OrteliusWorldMap"
onmouseover="OrteliusWorldMap.width='300';OrteliusWorldMap.height='200';"
onmouseout="OrteliusWorldMap.width='150';OrteliusWorldMap.height='100';" />

JSFiddle Link

编辑:

借助name 属性,您现在可以随心所欲地更改它。

<div class="profiles">
    <ul>
        <li class="portraitsLeft" id="one" align="left">
            <a href="google.com" onmouseover="OrteliusWorldMap.width=150;" onmouseout="OrteliusWorldMap.width=100">
                <img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/OrteliusWorldMap.jpeg" width="100" name="OrteliusWorldMap">
            </a> 
            Character 1
        </li>
    </ul>
</div>

JSFiddle Link.

尝试在别处使用this 的问题是this 将尝试引用当前的HTML 元素。有关this 以及onMouseOver event works 的更多信息,请参阅以下内容。

【讨论】:

  • 谢谢!这真的很有帮助 + 我今天学到了一些新的和方便的东西!
【解决方案2】:

为什么不使用CSS

<img id="image" src="../project/images/portraits/img1.jpg" align="middle" alt="Img1"/>

#image{
    width:50px;
    height:50px;
}

#image:hover{
    width:100px;
    height:100px;
}

JSFiddle Demo

或者您可以将代码更改为:

<img id="image" src="https://www.google.com/images/srpr/logo11w.png" onMouseOver="this.style.width='200px'" onMouseOut="this.style.width='100px'" alt="Img1"/>

使用this.style.width='200px'

JSFiddle Demo

【讨论】:

    【解决方案3】:

    除了 CSS,你也可以走 jQuery 路线:

    $("img").mouseover(function() {
        $("img").css({ width: '150px', height: '150px' });
    });
    

    如果您愿意,这还允许您在悬停时添加一些动画。

    这需要您在 HTML 中包含 jQuery 包:

    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    

    【讨论】:

      【解决方案4】:

      你可以这样尝试:

      <img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/OrteliusWorldMap.jpeg" alt="test" />
      
      img {
          width: 100px;
      }
      img:hover {
          width: 300px;
      }
      

      Working DEMO

      【讨论】:

      【解决方案5】:
      You can try using Javascript, without using any "this" keyword.
      
      <html>
       <body>
          <ul>
             <li>
                <a href="" onmouseover="img_increase()" onmouseout="img_decrease()">Character</a>         
                <img id="image" src="../project/images/portraits/img1.jpg" width="100" >          
             </li>
          </ul>
      
         <script>
      
              function img_increase()
              {
                  document.getElementById("image").style.width="150px";
              } 
      
              function img_decrease()
              {
                 document.getElementById("image").style.width="100px";
              }
      
          </script>
      
       </body>
      
      </html>
      

      【讨论】:

        【解决方案6】:

        'this' 是大多数编程语言(包括 java 和 javascript)使用的关键字。 'this' 关键字用于指向当前对象。 'this' 始终是对调用方法(函数)的对象的引用。

        【讨论】:

          猜你喜欢
          相关资源
          最近更新 更多
          热门标签