国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

基于angularjs實現圖片放大鏡效果

2019-11-20 09:07:06
字體:
來源:轉載
供稿:網友

前言

一開始打算用原生的angularjs寫,但是發現用原生angularjs,無論如何都不能獲取里面圖片的寬度和高度,因為angularjs內置的jquery方法里沒有winth()  、height()方法。

最好我還是引入了jquery,在同一scope上綁定了寬度高度。下面上源碼,順便我會把里面的一些注意的點說一下。

效果圖

首先說明下

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>

總結

以上就是這篇文章的全部內容,不知道大家都學會了嗎?希望這篇文章對大家的學習或者工作能帶來一定幫助,如果有問題歡迎大家留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富蕴县| 建始县| 新源县| 徐水县| 永城市| 莆田市| 望奎县| 攀枝花市| 高唐县| 新绛县| 旺苍县| 千阳县| 拜城县| 米泉市| 武定县| 禄丰县| 黄冈市| 白银市| 买车| 江阴市| 来安县| 五台县| 罗田县| 乐山市| 西吉县| 长治县| 砀山县| 阜新市| 德惠市| 海淀区| 固阳县| 泰州市| 泰宁县| 沙雅县| 拉萨市| 阿巴嘎旗| 玉林市| 栾城县| 栾城县| 麦盖提县| 大城县|