ionic开发——点击图片查看大图

html代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="https://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <style>
        .LightBox {
            z-index: 999;
            background: black;
            position: fixed;
            width: 100%;
            height: 100%;
            top: 0px;
            left: 0px;
        }
        
        .LightBox .ImgViewer {
            width: 100%;
            height: 100%;
            position: relative;
        }
        
        .LightBox .ImgViewer img {
            max-width: 100%;
            max-height: 100%;
            left: 50%;
            position: absolute;
            top: 50%;
        }
    </style>
    <script src="js/index.js"></script>
</head>
<body ng-app="myApp">
    <img ng-click="showBigImage('images/1.jpg')" src="images/1.jpg" />
    <img ng-click="showBigImage('images/2.jpg')" src="images/2.jpg" />
    <com-image-box></com-image-box>
</body>
</html>


JS代码:

var app = angular.module("myApp", []);
//通用 comImageBox 点击查看大图
app.directive('comImageBox', [function() {
    var comImageBox = {
        restrict: 'E',
        template: '<div class="LightBox" style="display:none;" ng-click="hideBigImage()"><div class="ImgViewer"><img src="{{Url}}"></div></div>',
        transclude: false,
        templateNamespace: 'html',
        scope: false,
        link: {
            pre: function preLink(scope, element, attrs, controller) {
                //图片大小
                var elWidth, elHeight;
                //元素大小
                var outWidth, outHeight;
                // 当前操作模式 pinch:缩放 swipe:滑动
                var mode = '';

                // 双指触摸点的距离 (缩放模式)
                var distance = 0;
                var initialDistance = 0;

                // 图片缩放参数
                var scale = 1;
                var relativeScale = 1;
                var initialScale = 1;
                var maxScale = parseInt(attrs.maxScale, 10);
                if (isNaN(maxScale) || maxScale <= 1) {
                    maxScale = 20;
                }

                // position of the upper left corner of the element
                var positionX = 0;
                var positionY = 0;

                var initialPositionX = 0;
                var initialPositionY = 0;

                // central origin (缩放模式)
                var originX = 0;
                var originY = 0;

                // start coordinate and amount of movement (滑动模式)
                var startX = 0;
                var startY = 0;
                var moveX = 0;
                var moveY = 0;


                outWidth = $(".LightBox").width();
                outHeight = $(".LightBox").height();


                scope.Url = "";
                scope.bigImage = false;

                //显示图片
                scope.showBigImage = function(imageName) {
                    scope.Url = imageName;
                    scope.bigImage = true;

                    $(".LightBox").show(150, function() {
                        $("ion-header-bar").hide();
                        $(".Main .tab-nav").hide();
                        $(".LightBox .ImgViewer img").each(function() {

                            $(this).css({
                                width: "100%"
                            });

                            elWidth = $(this).width();
                            elHeight = $(this).height();
                            //居中
                            $(this).css({

                                marginLeft: elWidth / 2 * (-1),
                                marginTop: elHeight / 2 * (-1)
                            })
                        });
                    });

                    //console.info(element.find("img"));
                    //每次点击放大图片之后需要绑定事件
                    element.find("img").on('touchstart', touchstartHandler);
                    element.find("img").on('touchmove', touchmoveHandler);
                    element.find("img").on('touchend', touchendHandler);
                };
                //隐藏图片
                scope.hideBigImage = function() {
                    scope.bigImage = false;
                    $(".LightBox").hide(200);
                    // 当前操作模式 pinch:缩放 swipe:滑动
                    mode = '';

                    // 双指触摸点的距离 (缩放模式)
                    distance = 0;
                    initialDistance = 0;

                    // 图片缩放参数
                    scale = 1;
                    relativeScale = 1;
                    initialScale = 1;
                    maxScale = parseInt(attrs.maxScale, 10);
                    if (isNaN(maxScale) || maxScale <= 1) {
                        maxScale = 20;
                    }

                    // position of the upper left corner of the element
                    positionX = 0;
                    positionY = 0;

                    initialPositionX = 0;
                    initialPositionY = 0;

                    // central origin (缩放模式)
                    originX = 0;
                    originY = 0;

                    // start coordinate and amount of movement (滑动模式)
                    startX = 0;
                    startY = 0;
                    moveX = 0;
                    moveY = 0;

                    transformElement();
                };


                /****************************************** 图片缩放功能开始 20161104 qinxiankang 添加 ***************************/






                /**
                 * @param {object} 点击开始,初始化
                 */
                function touchstartHandler(evt) {
                    //console.info("touchstart");
                    var touches = evt.originalEvent ? evt.originalEvent.touches : evt.touches;

                    startX = touches[0].clientX;
                    startY = touches[0].clientY;
                    initialPositionX = positionX;
                    initialPositionY = positionY;
                    moveX = 0;
                    moveY = 0;
                }

                /**
                 * @param {object} 手指移动
                 */
                function touchmoveHandler(evt) {
                    //console.info("touch move");
                    var touches = evt.originalEvent ? evt.originalEvent.touches : evt.touches;
                    var leftX = positionX - elWidth * scale / 2 + outWidth / 2;
                    var rightX = outWidth - (leftX + elWidth * scale);
                    var topY = positionY - elHeight * scale / 2 + outHeight / 2;
                    var bottomY = outHeight - (topY + elHeight * scale);



                    if (mode === '') {
                        if (touches.length === 1) {

                            mode = 'swipe';

                        } else if (touches.length === 2) {

                            mode = 'pinch';

                            initialScale = scale;
                            initialDistance = getDistance(touches);
                            originX = touches[0].clientX -
                                parseInt((touches[0].clientX - touches[1].clientX) / 2, 10) -
                                element.find("img")[0].offsetLeft - initialPositionX;
                            originY = touches[0].clientY -
                                parseInt((touches[0].clientY - touches[1].clientY) / 2, 10) -
                                element.find("img")[0].offsetTop - initialPositionY;

                        }
                    }

                    if (mode === 'swipe') {
                        //移动
                        evt.preventDefault();
                        moveX = touches[0].clientX - startX;
                        moveY = touches[0].clientY - startY;

                        positionX = initialPositionX + moveX;
                        positionY = initialPositionY + moveY;

                        transformElement();

                        //左右有空余,左右间距相同,禁止上下滑动
                        if (leftX > 0 && rightX > 0) {
                            positionX = 0;
                            transformElement();
                        }
                        //上下都有空余,禁止左右滑动
                        if (topY > 0 && bottomY > 0) {
                            positionY = 0;
                            transformElement();
                        }



                    } else if (mode === 'pinch') {
                        //缩放
                        evt.preventDefault();
                        distance = getDistance(touches);
                        relativeScale = distance / initialDistance;

                        //


                        scale = relativeScale * initialScale;

                        positionX = originX * (1 - relativeScale) + initialPositionX + moveX;
                        positionY = originY * (1 - relativeScale) + initialPositionY + moveY;

                        transformElement();
                        positionX = 0;
                        positionY = 0;
                        transformElement();
                    }



                    //console.info(leftX, topY, rightX, bottomY);
                }

                /**
                 * @param {object} 点击结束
                 */
                function touchendHandler(evt) {
                    //console.info("图片大小", elWidth, elHeight);
                    //console.info("容器大小", outWidth, outHeight);

                    //console.info("touch end");
                    var touches = evt.originalEvent ? evt.originalEvent.touches : evt.touches;

                    if (mode === '' || touches.length > 0) {
                        return;
                    }
                    //缩放比例小于原比例
                    if (scale < 1) {

                        scale = 1;
                        positionX = 0;
                        positionY = 0;

                    } else if (scale > maxScale) {
                        //缩放比例过大
                        scale = maxScale;
                        relativeScale = scale / initialScale;
                        positionX = originX * (1 - relativeScale) + initialPositionX + moveX;
                        positionY = originY * (1 - relativeScale) + initialPositionY + moveY;

                    }
                    //} else {
                    //    //
                    //    if (positionX > 0) {
                    //        positionX = 0;
                    //    } else if (positionX < elWidth * (1 - scale)) {
                    //        positionX = elWidth * (1 - scale);
                    //    }
                    //    if (positionY > 0) {
                    //        positionY = 0;
                    //    } else if (positionY < elHeight * (1 - scale)) {
                    //        positionY = elHeight * (1 - scale);
                    //    }

                    //}

                    //

                    var leftX = positionX - elWidth * scale / 2 + outWidth / 2;
                    var rightX = outWidth - (leftX + elWidth * scale);
                    var topY = positionY - elHeight * scale / 2 + outHeight / 2;
                    var bottomY = outHeight - (topY + elHeight * scale);

                    if (leftX > 0 && rightX < 0) {
                        //leftX=0;
                        positionX = elWidth * scale / 2 - outWidth / 2;
                        transformElement();
                    } else if (leftX < 0 && rightX > 0) {
                        //rightX=0;
                        positionX = outWidth / 2 - elWidth * scale / 2;
                        transformElement();
                    }
                    if (topY < 0 && bottomY > 0) {
                        positionY = outHeight / 2 - elHeight * scale / 2;

                        transformElement();
                    } else if (topY > 0 && bottomY < 0) {
                        positionY = elHeight * scale / 2 - outHeight / 2;
                        transformElement();
                    }


                    leftX = positionX - elWidth * scale / 2 + outWidth / 2;
                    rightX = outWidth - (leftX + elWidth * scale);
                    topY = positionY - elHeight * scale / 2 + outHeight / 2;
                    bottomY = outHeight - (topY + elHeight * scale);
                    //console.info(leftX, rightX, topY, bottomY);
                    if (topY > 0 && bottomY > 0) {
                        //让上下边距相同,只允许左右滑动
                        //console.info(1);
                        positionY = 0;
                        transformElement();
                    }
                    if (leftX > 0 && rightX > 0) {
                        //console.info(2);
                        positionX = 0;
                        transformElement();
                    }
                    leftX = positionX - elWidth * scale / 2 + outWidth / 2;
                    rightX = outWidth - (leftX + elWidth * scale);
                    topY = positionY - elHeight * scale / 2 + outHeight / 2;
                    bottomY = outHeight - (topY + elHeight * scale);
                    //console.info(leftX, rightX,topY, bottomY);
                    transformElement(0.1);
                    mode = '';

                }

                /**
                 * @param {Array} 双指touch位置
                 * @return {number} 
                 */
                function getDistance(touches) {
                    var d = Math.sqrt(Math.pow(touches[0].clientX - touches[1].clientX, 2) +
                        Math.pow(touches[0].clientY - touches[1].clientY, 2));
                    return parseInt(d, 10);
                }

                /**
                 * @param {number} 动画时间
                 */
                function transformElement(duration) {
                    //console.info("transform");
                    var transition = duration ? 'all cubic-bezier(0,0,.5,1) ' + duration + 's' : '';
                    var matrixArray = [scale, 0, 0, scale, positionX, positionY];
                    var matrix = 'matrix(' + matrixArray.join(',') + ')';

                    element.find("img").css({
                        '-webkit-transition': transition,
                        transition: transition,
                        '-webkit-transform': matrix + ' translate3d(0,0,0)',
                        transform: matrix
                    });
                }
                /****************************************** 图片缩放功能结束****************************/

            },

            post: function postLink(scope, element, attrs, controller) {}
        }
    };
    return comImageBox;
}])




本文章引自:https://github.com/halfopen/frontend-demos 中image-zooming

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mickey_于浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值