最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

輕松實(shí)現(xiàn)JavaScript圖片切換

 更新時間:2016年01月12日 16:48:42   作者:Superpan2015  
這篇文章主要幫助大家輕松實(shí)現(xiàn)JavaScript圖片切換,內(nèi)容很精簡,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家介紹JavaScript圖片切換的實(shí)現(xiàn)方法,分享給大家供大家參考,具體內(nèi)容如下

效果圖:

網(wǎng)頁看到非常常見的一個圖片切換效果:在淘寶、JD等購物時,介紹產(chǎn)品的圖片會有多張,一般是顯示一張,底下有一排小圖片,鼠標(biāo)放到小圖片上大圖片會切換.參考vivo X5M 移動4G手機(jī) .下面記錄一下實(shí)現(xiàn)的過程.

1. getElementById()

該方法是操作dom非常常用的一個方法,比如有一p標(biāo)簽,id設(shè)為pid,通過getElementById(“pid”)就可以對該元素進(jìn)行操作.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>demo</title>
  <script type="text/javascript">
    function changeText(){
      document.getElementById("pid").innerHTML
      ="It works!";
    }
  </script>
</head>
<body>
  <p id="pid" onmouseover="changeText()">Hello word!</p>
</body>
</html>

上面代碼中在body中寫了一個p標(biāo)簽,id為pid,當(dāng)鼠標(biāo)放到p標(biāo)簽上方的時候觸發(fā)onmouseover事件,執(zhí)行changeText()方法,將p標(biāo)簽內(nèi)的文檔改變.

2. setAttribute()和getAttribute()

getAttribute()方法用于獲取某一屬性的值,setAttribute()方法用于給某一屬性賦值。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>demo</title>
  <script type="text/javascript">
    function changeUrl(){
      var baiduurl=document.getElementById("aid");
      baiduurl.getAttribute("href");
      baiduurl.setAttribute("href", 
      "http://www.taobao.com");
      baiduurl.innerHTML="淘寶";
    }
  </script>
</head>
<body>
  <a  id="aid" onmouseover="changeUrl()">百度首頁</a>
</body>
</html>

上面代碼中,body中有一個a標(biāo)簽,通過getElementById()獲取a標(biāo)簽,baiduurl.getAttribute(“href”)的值為默認(rèn)的href屬性,通過baiduurl.setAttribute(“href”, “http://www.taobao.com“)設(shè)置以后,該屬性值改變.完整代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>on</title>
  <style type="text/css" media="screen">
  *{
     padding: 0;
  }
  body{
     font-family: 微軟雅黑;
  }
  #imgbox{
        width: 320px;
        height: 490px;
        padding: 10px;
        box-shadow: 5px;
        border: 1px solid #ccc;
        border-radius: 10px;
      }
 #phoneimg{
       padding: 10px;
       border-color: 1px solid #cccccc;
    }

  </style>


</head>
<body>
   <div id="imgbox">
    <img src="images/phone1.jpg" height="400" width="320" alt="phone" id="phoneimg">   
    <p id="decimg">phone image1</p>
   </div>

   <table>
    <tbody>
      <tr>
        <td width="50px">
          <img src="images/phone2.jpg" height="100" width="80" title="phone image2" alt="" onmouseover="changeImg(this)" ></td>
        <td width="50px">
          <img src="images/phone3.jpg" height="100" width="80" title="phone image3" alt=""onmouseover="changeImg(this)" ></td>
        <td width="50px">
          <img src="images/phone4.jpg" height="100" width="80" title="phone image4" alt=""onmouseover="changeImg(this)" ></td>
        <td width="50px">
          <img src="images/phone5.jpg" height="100" width="80" title="phone image5" alt=""onmouseover="changeImg(this)" ></td>

      </tr>
    </tbody>
   </table>

   <script type="text/javascript">
     function changeImg(whichpic){       
         var imgattr=whichpic.getAttribute("src");
         var phoneimg=document.getElementById("phoneimg");
         phoneimg.setAttribute("src",imgattr);
         var dectext=whichpic.getAttribute("title");
         document.getElementById("decimg").innerHTML=dectext;
     }
   </script>
</body>
</html>

下一步學(xué)習(xí)一下怎么實(shí)現(xiàn)局部放大,大家有什么好的方法嗎?可以一起探討。

相關(guān)文章

最新評論

玉环县| 嘉峪关市| 芜湖市| 定南县| 滦平县| 开远市| 湖北省| 法库县| 临沂市| 铜川市| 桦南县| 监利县| 舞钢市| 琼结县| 横峰县| 富民县| 特克斯县| 日土县| 沁水县| 当雄县| 盐城市| 卢氏县| 岗巴县| 揭西县| 子洲县| 永川市| 武城县| 定陶县| 安平县| 翁牛特旗| 涡阳县| 扬中市| 阿尔山市| 鹿泉市| 新晃| 正安县| 汝城县| 监利县| 怀远县| 塔城市| 益阳市|