轻松实现JavaScript图片切换


Posted in Javascript onJanuary 12, 2016

本文实例为大家介绍JavaScript图片切换的实现方法,分享给大家供大家参考,具体内容如下

效果图:

轻松实现JavaScript图片切换

网页看到非常常见的一个图片切换效果:在淘宝、JD等购物时,介绍产品的图片会有多张,一般是显示一张,底下有一排小图片,鼠标放到小图片上大图片会切换.参考vivo X5M 移动4G手机 .下面记录一下实现的过程.

1. getElementById()

该方法是操作dom非常常用的一个方法,比如有一p标签,id设为pid,通过getElementById(“pid”)就可以对该元素进行操作.

<!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标签,id为pid,当鼠标放到p标签上方的时候触发onmouseover事件,执行changeText()方法,将p标签内的文档改变.

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 href="http://www.baidu.com" id="aid" onmouseover="changeUrl()">百度首页</a>
</body>
</html>

上面代码中,body中有一个a标签,通过getElementById()获取a标签,baiduurl.getAttribute(“href”)的值为默认的href属性,通过baiduurl.setAttribute(“href”, “http://www.taobao.com“)设置以后,该属性值改变.完整代码:

<!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>

下一步学习一下怎么实现局部放大,大家有什么好的方法吗?可以一起探讨。

Javascript 相关文章推荐
给artDialog 5.02 增加ajax get功能详细介绍
Nov 13 Javascript
js弹出的对话窗口永远保持居中显示
Dec 15 Javascript
关于jQuery中.attr()和.prop()的问题探讨
Sep 06 Javascript
JS+CSS设置img在DIV中只显示Img垂直居中的部分
Oct 24 Javascript
原创jQuery弹出层插件分享
Apr 02 Javascript
聊一聊Vue.js过渡效果
Sep 07 Javascript
浅谈js中function的参数默认值
Feb 20 Javascript
深入理解Angular.JS中的Scope继承
Jun 04 Javascript
AngularJS 控制器 controller的详解
Oct 17 Javascript
jquery树形插件zTree高级使用详解
Aug 16 jQuery
JS数组进阶示例【数组的几种函数用法】
Jan 16 Javascript
JS图片预加载三种实现方法解析
May 08 Javascript
jQuery动画效果图片轮播特效
Jan 12 #Javascript
jQuery动画效果实现图片无缝连续滚动
Jan 12 #Javascript
jqueryMobile使用示例分享
Jan 12 #Javascript
WordPress中鼠标悬停显示和隐藏评论及引用按钮的实现
Jan 12 #Javascript
WordPress中利用AJAX技术进行评论提交的实现示例
Jan 12 #Javascript
基于JavaScript实现div层跟随滚动条滑动
Jan 12 #Javascript
JavaScript继承模式粗探
Jan 12 #Javascript
You might like
重料打造自己的“宝马”---第三代
2021/03/02 无线电
PHP判断数据库中的记录是否存在的方法
2014/11/14 PHP
getimagesize获取图片尺寸实例
2014/11/15 PHP
详解Laravel视图间共享数据与视图Composer
2016/08/04 PHP
php中strtotime函数性能分析
2016/11/20 PHP
HTML页面如何象ASP一样接受参数
2007/02/07 Javascript
让IE8支持DOM 2(不用框架!)
2009/12/31 Javascript
IE中createElement需要注意的一个问题
2010/07/13 Javascript
js+css使DIV始终居于屏幕中间 左下 左上 右上 右下的代码集合
2011/03/10 Javascript
设为首页和收藏的Javascript代码(亲测兼容IE,Firefox,chrome等浏览器)
2013/11/18 Javascript
jQuery动态改变图片显示大小(修改版)的实现思路及代码
2013/12/24 Javascript
JQuery的Ajax中Post方法传递中文出现乱码的解决方法
2014/10/21 Javascript
实例讲解JQuery中this和$(this)区别
2014/12/08 Javascript
微信小程序 rpx 尺寸单位详细介绍
2016/10/13 Javascript
AngularJS入门教程之数据绑定原理详解
2016/11/02 Javascript
jQuery实现简易的输入框字数计数功能示例
2017/01/16 Javascript
javascript history对象详解
2017/02/09 Javascript
浅谈React中的元素、组件、实例和节点
2018/02/27 Javascript
详解Vue源码学习之双向绑定
2019/04/10 Javascript
React+TypeScript+webpack4多入口配置详解
2019/08/08 Javascript
vue中利用three.js实现全景图的完整示例
2020/12/07 Vue.js
Python基础知识_浅谈用户交互
2017/05/31 Python
django文档学习之applications使用详解
2018/01/29 Python
Python实现FM算法解析
2019/06/18 Python
CSS3 transforms应用于背景图像的解决方法
2019/04/16 HTML / CSS
澳大利亚冲浪和时尚服装网上购物:SurfStitch
2017/07/29 全球购物
加热夹克:RAVEAN
2018/10/19 全球购物
UML设计模式笔试题
2014/06/07 面试题
艺术系大学生毕业个人自我评价
2013/09/19 职场文书
国际商务专业学生个人的自我评价
2013/09/28 职场文书
厂长助理岗位职责
2013/12/27 职场文书
中秋节主持词
2014/04/02 职场文书
旅游安全协议书
2014/04/21 职场文书
会计求职信
2014/05/29 职场文书
运动会闭幕式主持词
2015/07/01 职场文书
如何让你的Nginx支持分布式追踪详解
2022/07/07 Servers