需求是以上效果展示。话不多说,直接代码显示,不涉及代码优化。已实现功能为目的。

先看html部分:

<body>
<div class="dream" id="dream">
<div class="top">
<input type="button" value="上一组">
<input type="button" value="下一组">
</div>
<div class="footer">
<div class="b_left" id="b_left">
<img src="" alt="">
<span>图片描述加载中...</span>
<strong>图片数量计算中...</strong>
</div>
<div class="b_right" id="b_right">
<img src="" alt="">
<span>图片描述加载中...</span>
<strong>图片数量计算中...</strong>
</div>
</div>
</div>
</body>

然后是CSS部分:

     <style>
body{
background-color: #000;
}
*{
margin: 0;
padding: 0;
}
.dream{
width: 875px;
height: 500px;
background: url(bg.jpg)no-repeat;
margin: 50px auto;
}
.dream .top{
padding-top: 50px;
margin-left: 20px;
}
.dream .top input{
width: 75px;
border-radius: 5px;
background: #fff; }
.footer{
width: 810px;
height:350px;
background-color: #ccc;
margin: 10px 39px 0 25px;
}
.footer .b_left{
float: left;
width: 495px;
height: 350px;
background-color: #fff;
text-align: center;
position: relative;
}
.footer .b_left span,
.footer .b_right span{
position: absolute;
width: 90px;
bottom: 50px;
left: 50%;
margin-left:-45px;
font: 600 13px "微软雅黑";
}
.footer .b_right{
float:right;
width: 300px;
height: 350px;
background-color: #fff;
margin-left: 10px;
text-align: center;
position: relative;
}
.footer .b_left img{
position: absolute;
top:20px;
left: 20px;
width: 450px;
height: 250px; }
.footer .b_right img {
position: absolute;
top:20px;
right: 20px;
width: 250px;
height: 250px;
}
.footer .b_left strong,
.footer .b_right strong{
position: absolute;
bottom: 20px;
left: 20px;
font: 300 12px "宋体";
}
</style>

最后是JS部分:

 <script>
window.onload = function(){
var oDream = document.getElementById('dream');
var aInp = oDream.getElementsByTagName('input');//找到所有input
var oBlft = document.getElementById('b_left');
var oImg = oBlft.getElementsByTagName('img')[0];
var oSpan = oBlft.getElementsByTagName('span')[0];
var oStrong = oBlft.getElementsByTagName('strong')[0];
var oBright = document.getElementById('b_right');
var oImg2 = oBright.getElementsByTagName('img')[0];
var oSpan2 = oBright.getElementsByTagName('span')[0];
var oStrong2 = oBright.getElementsByTagName('strong')[0]; var arrUrl_l = ['img1/belle.jpg','img1/city.jpg','img1/flower.jpg','img1/rouse.jpg','img1/smail.jpg'];
var arrUrl_r =['img2/load.jpg','img2/pencil.jpg','img2/watch.jpg','img2/water.jpg'];
var arrText_l = ['第一组第1张', '第一组第2张', '第一组第3张', '第一组第4张', '第一组第5张'];
var arrText_r = ['第二组第1张','第二组第2张','第二组第3张','第二组第4张']; var num = 0; //初始化左边
oImg.src = arrUrl_l[num];
oSpan.innerHTML = arrText_l[num];
oStrong.innerHTML = (num+1) + ' / ' + arrUrl_l.length;
oImg.onclick = function(){
if(num == arrUrl_l.length){
num = 0;
}
oImg.src = arrUrl_l[num];
oSpan.innerHTML = arrText_l[num];
oStrong.innerHTML = (num + 1) + ' / ' + arrUrl_l.length;
num++;
} //初始化右边
oImg2.src = arrUrl_r[num];
oSpan2.innerHTML = arrText_r[num];
oStrong2.innerHTML = (num + 1) + ' / ' + arrUrl_r.length;
oImg2.onclick = function () {
if (num == arrUrl_r.length) {
num = 0;
}
oImg2.src = arrUrl_r[num];
oSpan2.innerHTML = arrText_r[num];
oStrong2.innerHTML = (num + 1) + ' / ' + arrUrl_r.length;
num++;
} //关联左右图片联动
for(var i = 0; i<aInp.length;i++){
aInp[i].index = i;
aInp[i].onclick = function(){
if (this.index == arrUrl_l.length) {
this.index = 0;
}
oImg.src = arrUrl_l[this.index];
oSpan.innerHTML = arrText_l[this.index];
oStrong.innerHTML = (this.index + 1) + ' / ' + arrUrl_l.length;
if (this.index == arrUrl_r.length) {
this.index = 0;
}
oImg2.src = arrUrl_r[this.index];
oSpan2.innerHTML = arrText_r[this.index];
oStrong2.innerHTML = (this.index + 1) + ' / ' + arrUrl_r.length;
this.index++;
}
} }
</script>

初学js之多组图片切换实例的更多相关文章

  1. 原生js实现多组图片切换

    这几天一直在练习原生js写效果,需要理清自己的逻辑,做了一个切换多组图片的效果: css样式: * { margin: 0; padding: 0; } body { background: #303 ...

  2. 极简的js点击组图切换效果

    程序员进行前端开发时,时常要用到点击切换组图的动画效果,网上确实有很多此类插件,但是都很麻烦,乌糟糟无数代码,有那个看的时间,自己都能把功能写完了.在这里我提供一段极简的js点击组图切换效果代码,包含 ...

  3. js鼠标滚轮滚动图片切换效果

    效果体验网址:http://keleyi.com/keleyi/phtml/image/12.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...

  4. js基础练习--控制多组图片切换

    js基础练习题,一个按钮控制两组图片的切换,做这题的时候我忽然想到了将num1.mun2……都存放在一个数组中,根据索引值匹配到对应相应组的图片,这样不管有多少组图片都简单的搞定切换,可惜js基础都没 ...

  5. js应用之实现图片切换效果

    数组的操作与应用 数组的定义 var 数组名=new Array(); //创建空数组 var 数组名=new Array(size);//创建指定数组长度的数组 var 数组名=new Array( ...

  6. JS案例练习:图片切换+切换模式

    先附图: CSS样式部分: <style> *{;} body{font-family:'Microsoft YaHei';} .menu{margin:20px auto 0; widt ...

  7. JS实现简单的图片切换效果

    使用图片进行点击切换效果 <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  8. js带缩略图的图片切换效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 2018.7.2 如何用js实现点击图片切换为另一图片,再次点击恢复到原图片

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. DNS跳转

    switch (window.location.hostname) { case "www.zcom.gov.cn" ://确定域名为 www.zcom.gov.cn //wind ...

  2. android读写SD卡封装的类

    参考了网上的一些资源代码,FileUtils.java: package com.example.test; import java.io.BufferedInputStream; import ja ...

  3. 大数据kafka视频教程 学习记录【B站尚硅谷 】

    视频地址: https://www.bilibili.com/video/av35354301/?p=1           2019/03/06 21:59 消息队列的内部实现: Kafka基础: ...

  4. 常用模块random,time,os,sys,序列化模块

    一丶random模块 取随机数的模块 #导入random模块 import random #取随机小数: r = random.random() #取大于零且小于一之间的小数 print(r) #0. ...

  5. 一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

    你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa  跨终端技术:HTML 5.CSS 3.JavaScri ...

  6. Unicode字符集

    Unicode字符集的出现是为了弥补ASCII码只能表示128个字符的限制.在实际应用中,如若我们想显示汉字或日文等等,显然使用ASCII是不可能的.Unicode占用了两个字节,即16位,能表示的字 ...

  7. CocoStudio UIButton setPressedActionEnabled(true) 子控件不跟着缩放

    具体情况是这样的:美术给了我 一个按钮的背景图片  一个按钮的文字图片,用背景图片创建一个button,然后把文字图片添加进去(注意关闭文字图片的交互功能) 设置UIButton setPressed ...

  8. PHP函数:method_exists和function_exists

    method_exists 检查类的方法是否存在 bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在于指定的ob ...

  9. python3基础10(操作日志)

    #!/usr/bin/env python# -*- coding:UTF-8 -*- import logging, time, os # 这个是日志保存本地的路径log_path = " ...

  10. js 中的 Math.ceil() Math.floor Math.round()

    alert(Math.ceil(25.9)); alert(Math.ceil(25.5)); alert(Math.ceil(25.1)); alert(Math.round(25.9)); ale ...