网站中常用的幻灯片和模态框,使用 HTML、JavaScript 与 CSS 来创建 Lightbox,类似相册预览功能。可以运用到视频网站,商城,相册上去

参考了菜鸟教程,有兴趣自己去看

HTML//写代码时,HTML记得包裹顺序就行,其他的往下写,搭建结构

 <!-- 图片改为你的图片地址 -->
<h2 style="text-align:center">Lightbox</h2> <div class="row">
<div class="column">
<img src="http://static.runoob.com/images/demo/demo1.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://static.runoob.com/images/demo/demo2.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://static.runoob.com/images/demo/demo3.jpg" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://static.runoob.com/images/demo/demo4.jpg" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div> <div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">&times;</span>
<div class="modal-content"> <div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="http://static.runoob.com/images/demo/demo1.jpg" style="width:100%">
</div> <div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="http://static.runoob.com/images/demo/demo2.jpg" style="width:100%">
</div> <div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="http://static.runoob.com/images/demo/demo3.jpg" style="width:100%">
</div> <div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="http://static.runoob.com/images/demo/demo4.jpg" style="width:100%">
</div> <a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a> <div class="caption-container">
<p id="caption"></p>
</div> <div class="column">
<img class="demo cursor" src="http://static.runoob.com/images/demo/demo1.jpg" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="http://static.runoob.com/images/demo/demo2.jpg" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="http://static.runoob.com/images/demo/demo3.jpg" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="http://static.runoob.com/images/demo/demo4.jpg" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
</div>
</div>
</div>

css//主要是利用浮动,定位,显示,背景,边框等把效果做出来

 body {
font-family: Verdana, sans-serif;
margin:;
} * {
box-sizing: border-box;
} .row > .column {
padding: 0 8px;
} .row:after {
content: "";
display: table;
clear: both;
} .column {
float: left;
width: 25%;
} /* 弹窗背景 */
.modal {
display: none;
position: fixed;
z-index:;
padding-top: 100px;
left:;
top:;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
} /* 弹窗内容 */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding:;
width: 90%;
max-width: 1200px;
} /* 关闭按钮 */
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
} .close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
} .mySlides {
display: none;
} .cursor {
cursor: pointer
} /* 上一页 & 下一页 按钮 */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
} /* 定位下一页按钮到右侧 */
.next {
right:;
border-radius: 3px 0 0 3px;
} /* 鼠标移动上去修改背景色为黑色 */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
} /* 页数(1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top:;
} img {
margin-bottom: -4px;
} .caption-container {
text-align: center;
background-color: black;
padding: 2px 16px;
color: white;
} .demo {
opacity: 0.6;
} .active,
.demo:hover {
opacity:;
} img.hover-shadow {
transition: 0.3s
} .hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}

JS//实现动态效果,主要是第一控制样式,第二是控制幻灯片,点击时能到对应的位子显示对应的图片内容

 <script>
//点击图片时打开模态框display来控制
function openModel()
{ document.getElementById("myModal").style.display="block";}
//当点击关闭按钮是将模态框关闭display为none;
function closeModal()
{ document.getElementById('myModal').style.display = "none";}
//当点击模态框外的地方关闭模态框
window.onclick=function(ev){
var model=document.getElementById('myModal')
if(ev.target!=="model"){
model.style.display = "none";
}
}
//设计一个显示幻灯片的函数
var slideIndex = 1;//默认显示第一张
function showSlides(){
//获得幻灯片和幻动片控制器
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
//获得每个幻灯片显示的标题
var captionText = document.getElementById("caption");
           slideIndex++;
 //判断万一输入的数字大于幻灯片数怎么办,让他回到第一张 

 if(slideIndex>slides.length) {slideIndex = 1}; 

 //反之万一小于呢,就让他回到最后一张(当然也可以自己决定) 
 if(n<1) {slideIndex =slides.length }; 
//先把所有幻灯片隐藏起来 for(var i=0;i<slides.length;i++){
slides[i].style.display="none"; }
//给幻灯片控制器一个样式,否则不知道是不是这个控制器有没有用
for (var j = 0; j < dots.length; j++) {
dots[j].className = dots[j].className.replace(" active", "");
}
//下面就是把当传入的值是哪个就让他显示哪个,slideIndex控制幻灯片(他可以是任何东西)
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
//把幻灯片标题显示过来
captionText.innerHTML = dots[slideIndex-1].alt;
setTimeout(showSlides, 1000); // 切换时间为 1 秒
};
//运行这个函数
showSlides(slideIndex);
//在注册两个前进后退的函数
function plusSlides(n) {
showSlides(slideIndex += n);
} function currentSlide(n) {
showSlides(slideIndex = n);
}
//用定时器可以设置自动播放
</script>

JS 实现图片模态框,幻灯片,跑马灯功能的更多相关文章

  1. 使用JS实现图片轮播滚动跑马灯效果

    我的第一篇文章.哈哈.有点小鸡冻.  之前在百度搜索"图片轮播"."图片滚动",结果都是那种可以左右切换的.也是我们最常见的那种.可能是搜索 关键字的问题吧. ...

  2. 使用bootstrap的JS插件实现模态框效果

    在上一篇文章中,我们使用 js+css 实现了模态框效果,在理解了模态框的基本实现方法和实现效果后,我们就要寻找更快捷的方法,又快又好的来完成模态框开发需求,从而节约时间,提高效率.一个好的轮子,不仅 ...

  3. js控制Bootstrap 模态框(Modal)插件

    js控制Bootstrap 模态框(Modal)插件 http://www.cnblogs.com/zzjeny/p/5564400.html

  4. Dom实例:数据自增、搜索框及跑马灯

    数据自增 功能:当点击add按扭后,div标签中的数据自动+1,实现网页的动态化 <!DOCTYPE html> <html lang="en"> < ...

  5. JS学习笔记(模态框JS传参)

    博主最近基于django框架的平台第一版差不多完成了 今天整理下开发过程中遇到的前端知识 基于前端bootstrap框架模态框传参问题 上前端html代码: <div class="m ...

  6. JS /CSS 实现模态框(注册和登录组件)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 使用js实现显示系统当前时间并实现倒计时功能并触发模态框(遮罩)功能

    常常在我们的网页中需要倒计时来触发一些函数,例如遮罩等,在本项目中,通过使用jquery,bootstrap,实现了显示系统当前时间,并且实现了倒计时的功能,倒计时实现后将会弹出模态框(遮罩层).模态 ...

  8. JavaScript进阶(八)JS实现图片预览并导入服务器功能

    JS实现导入文件功能       赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,共勉!(PS:此篇博文是自己在午饭时间所写,为此没吃午饭,这就是程序猿 ...

  9. 原生JS实现图片上传并预览功能

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

随机推荐

  1. hdu5336XYZ and Drops

    题意:给出r*c的网格,有的网格为空.有的有水.再给出一个爆炸点,从这个点向四周爆出四个水滴,若碰到水则融为一体,若碰到其它水滴直接跑过去互不影响.每秒可跑一格,若水中水滴数量超过4则爆开.问T秒后网 ...

  2. 分布式文件存储FastDFS(一)初识FastDFS

    一.FastDFS简单介绍 FastDFS是一款开源的.分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发.作为一个分布式文件系统,它对文件进行管理. ...

  3. MFC Wizard创建的空应用程序中各个文件内容的解析

    创建的MFC应用程序名为:wd,那么: 一.wd.h解析 // wd.h : main header file for the WD application // #if !defined(AFX_W ...

  4. shrio 加密/编码

    在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...

  5. Can’t connect to local MySQL server through socket ‘/tmp/mysql/mysql.sock’解决方法

    原因在于/tmp/mysql/mysql.sock不存在,为/usr/local/mysql/mysql.sock建立一个软连接到/tmp/mysql/mysql.sock即可. ln -s /usr ...

  6. 130.C++经典面试题 52-100

  7. CloudFoundry 云平台部署

    CloudFoundry云平台部署 CloudFoundry(TheOpenSourceCloudOperatingSystem)距离发布已经一年多了作为第一个开源的PaaS平台日臻成熟.在这一年里C ...

  8. java引用被设置为null的疑惑

    a=null; public class C { protected A webDigester = new A(" first one "); public void test( ...

  9. OPENCV(2) —— Basic Structures(二)

    Mat OpenCV C++ n-dimensional dense array class The class Mat represents an n-dimensional dense numer ...

  10. LRJ入门经典-0906最短公共父串305

    原题 LRJ入门经典-0906最短公共父串305 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 给定字符串A和字符串B,要求 ...