手写angular轮播图(手机端无缝版)

代码地址:

http://files.cnblogs.com/files/fxnet/%E6%89%8B%E5%86%99angular%E8%BD%AE%E6%92%AD%E5%9B%BE%EF%BC%88%E6%89%8B%E6%9C%BA%E7%AB%AF%E6%97%A0%E7%BC%9D%E7%89%88%EF%BC%89.zip

<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<script src="angular-1.2.19.min.js"></script>
<script src="angular-touch.js" type="text/javascript"></script>
<style>
body
{
font-family: 'Roboto' , sans-serif;
}
.lbt
{
overflow: hidden;
width: 100%;
position: relative;
}
ul
{
white-space: nowrap;
padding-left: 0;
list-style: none;
}
ul li
{
display: inline-block;
position: relative;
}
ul li img
{
display: block;
width: 100%;
height: 100%;
}
ul li label
{
position: absolute;
bottom: 10px;
left: 10px;
color: white;
background-color: black;
opacity: 0.8;
border-radius: 3px;
padding: 5px;
}
ul.lbtul
{
position: relative;
transition: 1s left;
left: 0;
margin: 0;
height: 120px;
}
ul.lbtul li
{
float: left;
}
ul.lbtnav
{
float: right;
}
ul.lbtnav li
{
margin-right: 10px;
border: 1px solid #696565;
width: 10px;
height: 10px;
border-radius: 5px;
}
ul.lbtnav li.selected
{
background-color: #696565;
}
</style>
</head>
<body ng-app="demo" ng-controller="DemoController">
<div ng-model="Data" rn-stepper class="lbt">
</div>
<div id="labSm">
</div>
</body>
<script>
angular.module('demo', ["ngTouch"]).controller('DemoController', function ($scope) {
$scope.Data = [
{ src: "http://17haidai.cn/asd/bigType/354.jpg", desp: "港澳台" },
{ src: "http://17haidai.cn/asd/bigType/288.jpg", desp: "泰国" },
{ src: "http://17haidai.cn/asd/bigType/156.jpg", desp: "美国" },
{ src: "http://17haidai.cn/asd/bigType/175.jpg", desp: "英国" },
{ src: "http://17haidai.cn/asd/bigType/176.jpg", desp: "德国" },
{ src: "http://17haidai.cn/asd/bigType/177.jpg", desp: "意大利" },
{ src: "http://17haidai.cn/asd/bigType/178.jpg", desp: "法国" },
];
})
.directive('rnStepper', function () {
return {
restrict: 'AE',
require: 'ngModel',
scope: {},
template: '<ul class="lbtul" ng-style="{width:ulSumWidth}" ng-touchmove="touchFunction($event)" ng-touchstart="touchFunction($event)" ng-touchend="touchFunction($event)">' +
'<li ng-repeat="one in Data" ng-style="{width:width}"><img ng-src="{{ one.src }}"/><label>{{ one.desp }}</label></li>' +
'</ul>' +
'<ul class="lbtnav"><li ng-repeat="one in DataMember" ng-click="qhimg()"></li></ul>',
link: function (scope, iElement, iAttrs, ngModelController) {
scope.nowPage = 1; //当前图片索引
scope.maxPage = 0; //数据总长度
scope.ulSumWidth = 0; //图片ul总宽度
scope.width = document.body.offsetWidth; //当前屏幕宽度
ngModelController.$render = function () {
scope.Data = ngModelController.$viewValue; //数据源
scope.DataMember = JSON.parse(JSON.stringify(scope.Data)); //分页数据源
var lastData = JSON.parse(JSON.stringify(scope.Data[scope.Data.length - 1]));
var fistData = JSON.parse(JSON.stringify(scope.Data[0]));
scope.Data.unshift(lastData);
scope.Data.push(fistData);
scope.maxPage = scope.Data.length; //数据总长度
scope.ulSumWidth = scope.maxPage * scope.width; //图片ul总宽度
//刚开始设置ul的滚动速度为0s,因为要先把第一张图片展示出来,然后再修改为1s
iElement.find("ul")[0].style.transition = "0s left";
iElement.find("ul")[0].style.left = -(scope.nowPage * scope.width);
setTimeout(function () {
iElement.find("ul")[0].style.transition = "1s left";
}, 2000);
//设置第一张图片为选中状态
setTimeout(function () {
iElement.find("ul")[1].children[0].className = "selected";
scope.liSelected = iElement.find("ul")[1].children[0];
}, 100);
//定时滚动图片
scope.LbInvert = setInterval(function () {
scope.Invert_RollImg();
}, 2000);
};
scope.qhimg = function () {
iElement.find("ul")[0].style.transition = "1s left";
var sumZ = this.$index * scope.width;
iElement.find("ul")[0].style.left = -((this.$index + 1) * scope.width) + "px";
scope.nowPage = this.$index;
if (scope.liSelected)
scope.liSelected.className = "";
iElement.find("ul")[1].children[scope.nowPage].className = "selected";
scope.liSelected = iElement.find("ul")[1].children[scope.nowPage];
scope.nowPage = scope.nowPage + 1;
clearInterval(scope.LbInvert);
scope.LbInvert = setInterval(function () {
scope.Invert_RollImg();
}, 2000);
}
scope.touchFunction = function (event) {
document.getElementById("labSm").innerHTML = event.type;
switch (event.type) {
case "touchstart":
scope.X = event.touches[0].clientX;
clearInterval(scope.LbInvert);
iElement.find("ul")[0].style.transition = "inherit";

if (scope.nowPage == 1)
iElement.find("ul")[0].style.left = -(scope.nowPage * scope.width) + "px";
else if (scope.nowPage == scope.maxPage - 2)
iElement.find("ul")[0].style.left = -(scope.nowPage * scope.width) + "px";

scope.Left = parseInt(iElement.find("ul")[0].style.left.replace("px", "") || 0);
break;
case "touchmove":
if (event.touches.length > 0) {
var moveX = 0;
scope.touchMoveX = event.touches[0].clientX;
if (event.touches[0].clientX > scope.X) {
moveX = scope.X - event.touches[0].clientX;
moveX = scope.Left - moveX;
scope.trun = "right";
}
else {
moveX = scope.X - event.touches[0].clientX;
moveX = scope.Left + -moveX;
scope.trun = "left";
}
iElement.find("ul")[0].style.left = moveX;
scope.moveX = moveX;
}
break;
case "touchend":
iElement.find("ul")[0].style.transition = "1s left";
scope.LbInvert = setInterval(function () {
scope.Invert_RollImg();
}, 2000);
if (Math.abs(scope.touchMoveX - scope.X) < scope.width / 3) {
iElement.find("ul")[0].style.left = -(scope.nowPage * scope.width) + "px";
return;
}
scope.nowPage = scope.trun == "left" ? scope.nowPage + 1 : scope.nowPage - 1;
var boolTrue = false;
if (scope.nowPage > scope.maxPage - 2) {
iElement.find("ul")[0].style.left = -(scope.nowPage * scope.width) + "px";
scope.nowPage = 1;
boolTrue = true;
}
else if (scope.nowPage <= 0) {
iElement.find("ul")[0].style.left = "0px";
scope.nowPage = scope.maxPage - 2;
boolTrue = true;
}
if (scope.liSelected)
scope.liSelected.className = "";
iElement.find("ul")[1].children[scope.nowPage - 1].className = "selected";
scope.liSelected = iElement.find("ul")[1].children[scope.nowPage - 1];

if (!boolTrue)
iElement.find("ul")[0].style.left = -(scope.nowPage * scope.width) + "px";
break;
}
}

//定时滚动图片
scope.Invert_RollImg = function () {
scope.width = scope.width;
iElement.find("ul")[0].style.left = -((iElement.find("ul")[0].style.left == "0px" ? 1 : (scope.nowPage + 1)) * scope.width) + "px";

if (scope.nowPage >= scope.maxPage - 2) {
scope.nowPage = 0;
setTimeout(function () {
iElement.find("ul")[0].style.transition = "0s left";
iElement.find("ul")[0].style.left = -(1 * scope.width) + "px";
}, 1000);
clearInterval(scope.LbInvert);
scope.LbInvert = setInterval(function () {
iElement.find("ul")[0].style.transition = "1s left";
scope.Invert_RollImg();
}, 2000);
}
scope.nowPage = scope.nowPage + 1;
if (scope.liSelected)
scope.liSelected.className = "";
iElement.find("ul")[1].children[scope.nowPage - 1].className = "selected";
scope.liSelected = iElement.find("ul")[1].children[scope.nowPage - 1];
}
}
};
});
</script>
</html>

转载于:https://www.cnblogs.com/fxnet/p/5910268.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值