基于angularjs實(shí)現(xiàn)圖片放大鏡效果
前言
一開(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)文章
在Angular項(xiàng)目使用socket.io實(shí)現(xiàn)通信的方法
這篇文章主要介紹了在Angular項(xiàng)目使用socket.io實(shí)現(xiàn)通信的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Angular 通過(guò)注入 $location 獲取與修改當(dāng)前頁(yè)面URL的實(shí)例
這篇文章主要介紹了Angular 通過(guò)注入 $location 獲取與修改當(dāng)前頁(yè)面URL的實(shí)例代碼,需要的朋友可以參考下2017-05-05
angularjs實(shí)現(xiàn)柱狀圖動(dòng)態(tài)加載的示例
本篇文章主要介紹了angularjs實(shí)現(xiàn)柱狀圖動(dòng)態(tài)加載的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Angularjs之如何在跨域請(qǐng)求中傳輸Cookie的方法
跨域傳輸Cookie是需要后臺(tái)和前臺(tái)同時(shí)做相關(guān)處理才能解決的,這篇文章主要介紹了Angularjs之如何在跨域請(qǐng)求中傳輸Cookie的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
angular5 httpclient的示例實(shí)戰(zhàn)
本篇文章主要介紹了angular5 httpclient的示例實(shí)戰(zhàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Facade Service暴露commands簡(jiǎn)化代碼邏輯提高可訪問(wèn)性組合性
在 Angular 應(yīng)用開(kāi)發(fā)中,使用 Facade Service 暴露 commands(命令)以及訂閱這些 commands 是一個(gè)常見(jiàn)的設(shè)計(jì)模式,本文將詳細(xì)介紹在 Facade Service 中如何實(shí)現(xiàn)這一目標(biāo),并深入探討相關(guān)細(xì)節(jié),以及通過(guò)實(shí)際示例進(jìn)行說(shuō)明2023-10-10
AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能,涉及AngularJS事件響應(yīng)實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Angular.js與Bootstrap相結(jié)合實(shí)現(xiàn)表格分頁(yè)代碼
最近一直在學(xué)習(xí)angularjs相關(guān)知識(shí),在學(xué)習(xí)過(guò)程中寫(xiě)了一個(gè)小demo,下面把代碼思路分享給大家,感興趣的朋友一起學(xué)習(xí)2016-04-04
AngularJS2中一種button切換效果的實(shí)現(xiàn)方法(二)
這篇文章主要介紹了AngularJS2中一種button切換效果的實(shí)現(xiàn)方法(二),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03

