最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于angularjs實(shí)現(xiàn)圖片放大鏡效果

 更新時(shí)間:2016年08月31日 15:15:27   作者:MakingChoice  
這篇文章給大家分享了angularjs實(shí)現(xiàn)購(gòu)物放大鏡效果的源碼實(shí)例,代碼介紹的很詳細(xì),有需要的可以參考借鑒,下面來(lái)一起看看。

前言

一開(kāi)始打算用原生的angularjs寫(xiě),但是發(fā)現(xiàn)用原生angularjs,無(wú)論如何都不能獲取里面圖片的寬度和高度,因?yàn)閍ngularjs內(nèi)置的jquery方法里沒(méi)有winth()  、height()方法。

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

效果圖

首先說(shuō)明下

1、首先使用了兩個(gè)同級(jí)指令,并在兩個(gè)同級(jí)指令間進(jìn)行通信,同級(jí)指令間通信,非常簡(jiǎn)單,就是不要讓同級(jí)的指令生成獨(dú)立的scope,并且在同一個(gè)作用域下就完成了。如果指令scope沒(méi)有特殊聲明,那么就是父scope。指令生成的模板沒(méi)有特殊意義,除非在特定的scope下編譯,默認(rèn)情況下,指令并不會(huì)創(chuàng)建新的子scope,更多的是使用父scope,也就是說(shuō),如果指令存在一個(gè)controller下,它會(huì)使用controller下的scope。

2、然后就是用$q來(lái)延遲異步獲取數(shù)據(jù),這個(gè)也可以看一下$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];
        });
        //右側(cè)容器內(nèi)照片寬度、高度
        scope.rightWidth=$(element).find("img").eq(scope.isActive).width();
        scope.rightHeight=$(element).find("img").eq(scope.isActive).height();
        //右側(cè)容器寬度、高度
        scope.rightBoxWidth=$(element).width();
        scope.rightBoxHeight=$(element).height();
        //移動(dòng)比例
        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>

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容,不知道大家都學(xué)會(huì)了嗎?希望這篇文章對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定幫助,如果有問(wèn)題歡迎大家留言交流。

相關(guān)文章

最新評(píng)論

浦江县| 兰西县| 宝兴县| 曲周县| 邵武市| 霍城县| 平和县| 包头市| 华池县| 嘉祥县| 拜城县| 利川市| 卢龙县| 明溪县| 宁阳县| 当涂县| 平武县| 古浪县| 佳木斯市| 丰镇市| 资溪县| 启东市| 杭州市| 肇州县| 蓝山县| 阜康市| 静宁县| 叙永县| 湄潭县| 沿河| 榆中县| 诏安县| 通榆县| 永城市| 赤水市| 尼玛县| 玛纳斯县| 东宁县| 巴林右旗| 建瓯市| 竹北市|