导读
项目已上传到我的码云,假如有须要的,能够自行下载:项目原码
本日,应产物司理的请求,当我们点击左按钮时,高低都显现第六张图片;当我们点击右按钮时,高低读显现第-1张图片。
触及的框架
- 你能够将项目down下来,运用 webstorm或HBuilder翻开。
- 运用JQuery将静态的HTML变得有灵魂。
- 运用vue.js完成数据的双向绑定
- 运用bootstrap来美化静态页面
- 运用layui的更美地弹出信息,比方已到达末了一张了,没法左移
页面设想
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片的滑动</title>
<link href="../css/bootstrap.css" rel="stylesheet">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/layer.css" rel="stylesheet">
<link href="../css/code.css" rel="stylesheet">
<script src="../js/jQuery.js" type="text/javascript"></script>
<script src="../js/bootstrap.js" type="text/javascript"></script>
<script src="../js/bootstrap.min.js" type="text/javascript"></script>
<script src="../js/vue.js" type="text/javascript"></script>
<script src="../js/layui.js" type="text/javascript"></script>
<script src="../js/layui.all.js" type="text/javascript"></script>
<script src="../js/imgSlide.js" type="text/javascript"></script>
</head>
<body>
<h2 style="width: 100%;text-align: center;margin-top: 20px">图片挪动</h2>
<div class="containsImg">
<div class="container" style="width:36%;height: 400px;position: relative;margin: 1% 32%;border-radius: 5px ">
<img :src="upImg.paths" :title="upImg.name" class="img-rounded" style="width: 100%;height: 100%;">
</div>
<div class="container" style="width:36%;height: 90px;position: relative;margin: 0 32%;border-radius: 5px ">
<img src="../imgs/left.png" class="img-rounded" onclick="fns.toLeft()" style="width: 6%;height: 100%;margin-right: 2%;float: left;">
<img v-for="it in images" :src="it.paths" :title="it.name" onclick="fns.toUp(this)" onmouseover="fns.toUp(this)" class="img-rounded"
style="width: 16%;height: 100%;margin-right: 1%;float: left;">
<img src="../imgs/right.png" class="img-rounded" onclick="fns.toRight()" style="width: 6%;height: 100%;float: left;margin-left: 1%">
</div>
</div>
</body>
</html>
js页面
/**
* @author zby
* @description 图片挪动
*/
/**
* 自挪用函数
* 函数表达式能够 "自挪用"。
* 自挪用表达式会自动挪用。
* 假如表达式背面紧跟 () ,则会自动挪用。
* 不能自挪用声明的函数。
* 经由过程增加括号,来申明它是一个函数表达式:
* (function () {
* var x = "Hello!!"; // 我将挪用本身
* })();
*/
(function (document, window, $) {
'use strict';
var vm = {}, length = 5;
var imgSrcs = new Array();
var layer = {}
/**
* jQuery信息初始化
*/
$(function () {
var fns = {
/**
* 设想思绪,图片左移
* 比方有六张图片,起首展现1,5折五张图片,向左挪动一次,展现2,6张图片,以此类推。。。
*/
toLeft: function () {
if (imgSrcs.length > 5) {
if (length < imgSrcs.length) {
length++;
vm.images = imgSrcs;
vm.images = vm.images.slice(length - 5, length);
//下标减1
vm.upImg = imgSrcs[length - 1];
} else {
length = imgSrcs.length;
layer.msg("已到达末了一张了,没法左移", {time: 800});
}
} else {
layer.msg("小图已悉数展现,没法左移", {time: 800});
}
},
/**
*设想思绪,图片右移
*比方有六张图片,起首展现2,6折五张图片,向右挪动一次,展现1,5张图片,以此类推。。。
*/
toRight: function () {
if (imgSrcs.length > 5) {
if (length > 5) {
length--;
vm.images = imgSrcs;
vm.images = vm.images.slice(length - 5, length);
//下标减5
vm.upImg = imgSrcs[length - 5];
} else {
length = 5;
layer.msg("已到达第一张了,没法右移", {time: 800});
}
} else {
layer.msg("小图片已悉数展现,没法右移", {time: 800});
}
},
toUp: function (me) {
vm.upImg = {
paths: $(me).attr("src"),
name: $(me).attr("title")
}
}
}
/**
* 变量、要领提拔,也就是说,能够在定义之前运用该变量和要领
*/
init();
//变量晋级为window对象,fns是局部变量,将其提拔为全局变量,即Windows变量
//全局变量可应用于页面上的一切剧本。
window.fns = fns;
});
/**
* 初始化数据
*/
function init() {
vm = new Vue({
el: ".containsImg",
data: {
images: {},
upImg: {}
},
})
imgSrcs.push({paths: "../imgs/1.jpg", name: "幽静竹林"});
imgSrcs.push({paths: "../imgs/2.jpg", name: "旭日暮景"});
imgSrcs.push({paths: "../imgs/3.jpg", name: "山岳湖水"});
imgSrcs.push({paths: "../imgs/4.jpg", name: "月下荡人"});
imgSrcs.push({paths: "../imgs/6.jpg", name: "蓝天白云水悠悠"});
imgSrcs.push({paths: "../imgs/7.jpg", name: "峭壁临水"});
imgSrcs.push({paths: "../imgs/8.jpg", name: "青山绿树"});
imgSrcs.push({paths: "../imgs/9.jpg", name: "通亮春光"});
imgSrcs.push({paths: "../imgs/10.jpg", name: "银装素裹"});
imgSrcs.push({paths: "../imgs/11.jpg", name: "雾凇雪路"});
imgSrcs.push({paths: "../imgs/12.jpg", name: "柏树绿水"});
vm.images = imgSrcs;
if (vm.images.length > 0) {
vm.upImg = {
name: imgSrcs[0].name,
paths: imgSrcs[0].paths,
}
if (vm.images.length > 5) {
vm.images = vm.images.slice(0, 5);
}
}
layui.use('layer', function () {
layer = layui.layer;
});
}
})(document, window, jQuery)
终究到达的结果
- 当我们点击向左挪动时,上面的大图也会跟着转变;假如到达末了一张了,就提醒没法左移。
- 当我们点击向右挪动时,上面的大图也会跟着转变;假如到达第一张了,就提醒没法右移。
*当鼠标滑过下面的小图时,上面的大图也会跟着转变。