基于angularjs实现图片放大镜效果


Posted in Javascript onAugust 31, 2016

前言

一开始打算用原生的angularjs写,但是发现用原生angularjs,无论如何都不能获取里面图片的宽度和高度,因为angularjs内置的jquery方法里没有winth()  、height()方法。

最好我还是引入了jquery,在同一scope上绑定了宽度高度。下面上源码,顺便我会把里面的一些注意的点说一下。

效果图

基于angularjs实现图片放大镜效果

首先说明下

1、首先使用了两个同级指令,并在两个同级指令间进行通信,同级指令间通信,非常简单,就是不要让同级的指令生成独立的scope,并且在同一个作用域下就完成了。如果指令scope没有特殊声明,那么就是父scope。指令生成的模板没有特殊意义,除非在特定的scope下编译,默认情况下,指令并不会创建新的子scope,更多的是使用父scope,也就是说,如果指令存在一个controller下,它会使用controller下的scope

2、然后就是用$q来延迟异步获取数据,这个也可以看一下$q的用法。

源码示例

<!DOCTYPE html>
<html lang="en" ng-app="magnifierAPP">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="lib/angular.min.js" type="text/javascript"></script>
  <script src="lib/angular-animate.js" type="text/javascript"></script>
  <script src="lib/jquery-2.1.4.min.js" type="text/javascript"></script>
</head>
<style>
  *{
    padding: 0px;
    margin: 0px;
  }
  .content{
    width: 800px;
    height: 400px;
    position: relative;
    border: 1px solid red;
  }
  .left{
    width: 310px;
    height: 380px;
  }
  .top{
    width: 310px;
    height: 310px;
    border: 1px solid blue;
    cursor: pointer;
  }
  .top img{
    width: 310px;
    height: 310px;
  }
  .bottom{
    position: relative;
    width: 310px;
    height: 60px;
    border: 1px solid black;
  }
  .bottom img{
    display: inline-block;
    width: 60px;
    height: 60px;
    float: left;
    margin: 0 30px;
    cursor: pointer;
  }
  .right{
    border: 1px solid ;
    width: 300px;
    height: 300px;
    position: absolute;
    left: 400px;
    top: 20px;
    overflow: hidden;
  }
  .right img{
    position: absolute;
    width: 700px;
    height: 600px;
  }
  .show{
    display: block;
  }
  .hidden{
    display: none;
  }
</style>
<body>
<div ng-controller="magnifierController">
  <div class="content">
    <div class="left" ng-init="isActive=0">
      <div>{{x}}+{{y}}</div>
      <div magnifier-top></div>
      <div class="bottom" >
        <img src="img/blue_1.jpg" alt="1" ng-mouseover="isActive=0"/>
        <img src="img/yellow_1.jpg" alt="1" ng-mouseover="isActive=1"/>
      </div>
    </div>
    <div magnifier-right>
      <div>{{x}}+{{y}}</div>
    </div>
  </div>
  <script type="text/ng-template" id="magnifier-top.html">
    <div class="top" ng-mousemove="getPosition($event.offsetX,$event.offsetY)" ng-mouseover="isshow=true" ng-mouseleave="isshow=false">
      <img src="img/blue_2.jpg" alt="0" ng-class="{true:'show',false:'hidden'}[isActive==0]"/>
      <img src="img/yellow_2.jpg" alt="1" ng-class="{true:'show',false:'hidden'}[isActive==1]"/>
    </div>
  </script>

  <script type="text/ng-template" id="magnifier-right.html" >
    <div class="right" >
      <img src="{{img1.src}}" alt="1" ng-class="{true:'show',false:'hidden'}[isActive==0]" id="img1"/>
      <img src="{{img2.src}}" alt="1" ng-class="{true:'show',false:'hidden'}[isActive==1]"/>
    </div>
  </script>
</div>
</body>
<script>
  var app=angular.module('magnifierAPP',[]);
  app.controller('magnifierController',['$scope', function ($scope) {

  }]);
  app.directive('magnifierRight',['readJson',function (readJson) {
    return {
      restrict: 'EA',
      replace:true,
      templateUrl:'magnifier-right.html',

      link: function (scope,element,attr) {
        var promise=readJson.getJson();
        promise.then(function (data) {
          scope.img1=data[0];
          scope.img2=data[1];
        });
        //右侧容器内照片宽度、高度
        scope.rightWidth=$(element).find("img").eq(scope.isActive).width();
        scope.rightHeight=$(element).find("img").eq(scope.isActive).height();
        //右侧容器宽度、高度
        scope.rightBoxWidth=$(element).width();
        scope.rightBoxHeight=$(element).height();
        //移动比例
        var radX=(scope.rightWidth-scope.rightBoxWidth)/scope.topWidth;
        var radY=(scope.rightHeight-scope.rightBoxHeight)/scope.topHeight;

        console.log(radX);
        console.log(radY);
        setInterval(function (){
          scope.$apply(function (){
            element.find("img").eq(scope.isActive).css({
              "left":-scope.x*radX+"px",
              "top":-scope.y*radY+"px"
            });
          })
        },30)
      }
    }
  }]);
  app.directive('magnifierTop',[function () {
    return{
      restrict:'EA',
      replace:true,
      templateUrl:'magnifier-top.html',
      link: function (scope,element,attr) {
        scope.topWidth=$(element).find("img").eq(scope.isActive).width();
        scope.topHeight=$(element).find("img").eq(scope.isActive).height();
        scope.getPosition= function (x,y) {
          scope.x=x;
          scope.y=y;
        }
      }
    }
  }]);
  app.factory('readJson',['$http','$q', function ($http,$q) {
    return{
      getJson: function (){
        var deferred=$q.defer();
        $http({
          method:'GET',
          url:'magnifier.json'
        }).success(function (data, status, header, config) {
          deferred.resolve(data);
        }).error(function (data, status, header, config) {
          deferred.reject(data);
        });
        return deferred.promise;
      }
    }
  }]);
</script>
</html>

总结

以上就是这篇文章的全部内容,不知道大家都学会了吗?希望这篇文章对大家的学习或者工作能带来一定帮助,如果有问题欢迎大家留言交流。

Javascript 相关文章推荐
JavaScript 解析Json字符串的性能比较分析代码
Dec 16 Javascript
jQuery教程 $()包装函数来实现数组元素分页效果
Aug 13 Javascript
将input file的选择的文件清空的两种解决方案
Oct 21 Javascript
javascript获取xml节点的最大值(实现代码)
Dec 11 Javascript
原生js实现复制对象、扩展对象 类似jquery中的extend()方法
Aug 30 Javascript
js函数与php函数的区别实例浅析
Jan 12 Javascript
20170918 前端开发周报之JS前端开发必看
Sep 18 Javascript
微信小程序页面生命周期详解
Jan 31 Javascript
Angular4 反向代理Details实践
May 30 Javascript
详解在Vue中使用TypeScript的一些思考(实践)
Jul 06 Javascript
vue同步父子组件和异步父子组件的生命周期顺序问题
Oct 07 Javascript
node.js连接mysql与基本用法示例
Jan 05 Javascript
用AngularJS的指令实现tabs切换效果
Aug 31 #Javascript
简洁实用的BootStrap jQuery手风琴插件
Aug 31 #Javascript
AngularJS实现一次监听多个值发生的变化
Aug 31 #Javascript
利用Angularjs和bootstrap实现购物车功能
Aug 31 #Javascript
JavaScript String(字符串)对象的简单实例(推荐)
Aug 31 #Javascript
基于JavaScript实现鼠标向下滑动加载div的代码
Aug 31 #Javascript
Node.js配合node-http-proxy解决本地开发ajax跨域问题
Aug 31 #Javascript
You might like
PHP代码优化的53个细节
2014/03/03 PHP
PHP中session跨子域的三种实现方法
2016/07/25 PHP
Yii框架实现记录日志到自定义文件的方法
2017/05/23 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
2019/11/19 PHP
WEB高性能开发之疯狂的HTML压缩
2010/06/19 Javascript
如何设置iframe高度自适应在跨域情况下的可用方法
2013/09/06 Javascript
Jquery 获取指定标签的对象及属性的设置与移除
2014/05/29 Javascript
动态载入js提高网页打开速度的方法
2014/07/04 Javascript
使用jquery实现放大镜效果
2014/09/02 Javascript
5个JavaScript经典面试题
2014/10/13 Javascript
javascript中hasOwnProperty() 方法使用指南
2015/03/09 Javascript
对js中回调函数的一些看法
2016/08/29 Javascript
bootstrap下拉框动态赋值方法
2018/08/10 Javascript
微信小程序 JS动态修改样式的实现方法
2018/12/16 Javascript
NodeJS实现同步的方法
2019/03/02 NodeJs
[06:14]《辉夜杯》外卡赛附加赛 4支战队巡礼
2015/10/23 DOTA
[02:02]2018DOTA2亚洲邀请赛Mineski赛前采访
2018/04/04 DOTA
仅利用30行Python代码来展示X算法
2015/04/01 Python
详解Python读取配置文件模块ConfigParser
2017/05/11 Python
python urllib urlopen()对象方法/代理的补充说明
2017/06/29 Python
Python实现的根据文件名查找数据文件功能示例
2018/05/02 Python
Python2与Python3的区别实例总结
2019/04/17 Python
python爬虫爬取幽默笑话网站
2019/10/24 Python
keras使用Sequence类调用大规模数据集进行训练的实现
2020/06/22 Python
Python 随机按键模拟2小时
2020/12/30 Python
Python将QQ聊天记录生成词云的示例代码
2021/02/10 Python
美国紧身牛仔裤品牌:NYDJ
2017/05/24 全球购物
英国助听器购物网站:Hearing Direct
2018/08/21 全球购物
经济担保书范文
2014/04/02 职场文书
爱耳日活动总结
2014/04/30 职场文书
八一建军节演讲稿
2014/09/10 职场文书
2015年大学教师工作总结
2015/05/20 职场文书
四年级数学教学反思
2016/02/16 职场文书
Lakehouse数据湖并发控制陷阱分析
2022/03/31 Oracle
使用vue判断当前环境是安卓还是IOS
2022/04/12 Vue.js
JavaScript前端面试扁平数据转tree与tree数据扁平化
2022/06/14 Javascript