基于angularjs實現(xiàn)圖片放大鏡效果
前言
一開始打算用原生的angularjs寫,但是發(fā)現(xiàn)用原生angularjs,無論如何都不能獲取里面圖片的寬度和高度,因為angularjs內(nèi)置的jquery方法里沒有winth() 、height()方法。
最好我還是引入了jquery,在同一scope上綁定了寬度高度。下面上源碼,順便我會把里面的一些注意的點說一下。
效果圖

首先說明下
1、首先使用了兩個同級指令,并在兩個同級指令間進行通信,同級指令間通信,非常簡單,就是不要讓同級的指令生成獨立的scope,并且在同一個作用域下就完成了。如果指令scope沒有特殊聲明,那么就是父scope。指令生成的模板沒有特殊意義,除非在特定的scope下編譯,默認(rèn)情況下,指令并不會創(chuàng)建新的子scope,更多的是使用父scope,也就是說,如果指令存在一個controller下,它會使用controller下的scope。
2、然后就是用$q來延遲異步獲取數(shù)據(jù),這個也可以看一下$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();
//移動比例
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é)
以上就是這篇文章的全部內(nèi)容,不知道大家都學(xué)會了嗎?希望這篇文章對大家的學(xué)習(xí)或者工作能帶來一定幫助,如果有問題歡迎大家留言交流。
相關(guān)文章
在Angular項目使用socket.io實現(xiàn)通信的方法
這篇文章主要介紹了在Angular項目使用socket.io實現(xiàn)通信的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Angular 通過注入 $location 獲取與修改當(dāng)前頁面URL的實例
這篇文章主要介紹了Angular 通過注入 $location 獲取與修改當(dāng)前頁面URL的實例代碼,需要的朋友可以參考下2017-05-05
angularjs實現(xiàn)柱狀圖動態(tài)加載的示例
本篇文章主要介紹了angularjs實現(xiàn)柱狀圖動態(tài)加載的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
angular5 httpclient的示例實戰(zhàn)
本篇文章主要介紹了angular5 httpclient的示例實戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Facade Service暴露commands簡化代碼邏輯提高可訪問性組合性
在 Angular 應(yīng)用開發(fā)中,使用 Facade Service 暴露 commands(命令)以及訂閱這些 commands 是一個常見的設(shè)計模式,本文將詳細介紹在 Facade Service 中如何實現(xiàn)這一目標(biāo),并深入探討相關(guān)細節(jié),以及通過實際示例進行說明2023-10-10
AngularJS實現(xiàn)的錨點樓層跳轉(zhuǎn)功能示例
這篇文章主要介紹了AngularJS實現(xiàn)的錨點樓層跳轉(zhuǎn)功能,涉及AngularJS事件響應(yīng)實現(xiàn)錨點跳轉(zhuǎn)功能的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Angular.js與Bootstrap相結(jié)合實現(xiàn)表格分頁代碼
最近一直在學(xué)習(xí)angularjs相關(guān)知識,在學(xué)習(xí)過程中寫了一個小demo,下面把代碼思路分享給大家,感興趣的朋友一起學(xué)習(xí)2016-04-04
AngularJS2中一種button切換效果的實現(xiàn)方法(二)
這篇文章主要介紹了AngularJS2中一種button切換效果的實現(xiàn)方法(二),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03

