Javascript 如何在ng repeat循环中分配angularjs事件侦听器?
我对angularjs是新手。我正在尝试创建一个照片库,其中的初始页面是一张来自twitter和instagram的缩略图,使用angularjs ng重复循环创建。当用户将鼠标悬停在图像上时,图像会淡出,并显示一个按钮供用户单击。我使用ng mouseenter和ng mouseleave创建了悬停效果。但由于某种原因,当我将鼠标悬停在图像上时,所有图像都会受到影响。应该只针对我悬停的单个图像。我不确定我做错了什么。请帮忙!代码如下。这里还有一个在线代码链接:Javascript 如何在ng repeat循环中分配angularjs事件侦听器?,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,我对angularjs是新手。我正在尝试创建一个照片库,其中的初始页面是一张来自twitter和instagram的缩略图,使用angularjs ng重复循环创建。当用户将鼠标悬停在图像上时,图像会淡出,并显示一个按钮供用户单击。我使用ng mouseenter和ng mouseleave创建了悬停效果。但由于某种原因,当我将鼠标悬停在图像上时,所有图像都会受到影响。应该只针对我悬停的单个图像。我不确定我做错了什么。请帮忙!代码如下。这里还有一个在线代码链接: * { 框大小:边框框; 字
* {
框大小:边框框;
字体系列:无衬线;
}
h1,
氢{
左边距:10px;
}
.内部{
显示:内联块;
垂直对齐:顶部;
填充物:5px;
位置:相对位置;
}
.内部img{
宽度:100%;
}
.内部.外部标题{
颜色:黑色;
宽度:375px;
高度:125px;
背景色:#fc7cab;
显示器:flex;
}
.内部.外部标题.内部标题{
字体大小:14px;
字体系列:无衬线;
溢出:隐藏;
宽度:320px;
高度:70像素;
保证金:自动;
}
.内部。覆盖颜色{
位置:绝对位置;
排名:0;
底部:0;
左:0;
右:0;
保证金:自动;
背景色:#fff;
过渡:全部5秒;
}
.内部.覆盖文本{
边框:2px实心#7e7e7e;
颜色:#7e7e7e;
字体大小:10px;
位置:绝对位置;
排名:0;
底部:0;
左:0;
右:0;
宽度:70px;
高度:35px;
线高:35px;
保证金:自动;
边界半径:1px;
文本对齐:居中;
过渡:全部5秒;
{{title}}
{{myMessage}}
{{x.caption}
看法
//创建模块
var appModule=angular.module('appName',[]);
//创建根范围变量
appModule.run(
函数($rootScope){
$rootScope.title=“Taneleer演示”;
}
);
//创建控制器
控制器('appCtrl',函数($scope,$http){
$scope.overlycolorstyles={“不透明度”:0};
$scope.overlytextstyles={“不透明度”:0};
$scope.makeVisibile=函数(){
//警报(“测试”);
//document.getElementById(“覆盖颜色”).style.opacity=.75;
//document.getElementById(“覆盖文本”).style.opacity=1;
$scope.overlycolorstyles={“不透明度”:.75};
$scope.overlytextstyles={“不透明度”:1};
}
$scope.makeInvisible=函数(){
//警报(“测试”);
//document.getElementById(“覆盖颜色”).style.opacity=0;
//document.getElementById(“覆盖文本”).style.opacity=0;
$scope.overlycolorstyles={“不透明度”:0};
$scope.overlytextstyles={“不透明度”:0};
}
$http({
方法:“获取”,
url:“https://taneleer.composedcreative.com/api/v1/feed/a0329f16-9225-11e6-89bb-296a97b9d609/bb0429f6-f0ca-11e7-8f5d-d92739a9a53f"
}).然后(函数mySucces(响应){
$scope.myMessage=“成功!”;
$scope.response=响应;
$scope.meta=response.data.meta;
$scope.outsideData=response.data;
$scope.insideData=response.data.data;
$scope.links=response.data.links;
$scope.selfLink=response.data.links.self;
$scope.firstLink=response.data.links.first;
$scope.lastLink=response.data.links.last;
$scope.nextLink=response.data.links.next;
$scope.prevLink=response.data.links.prev;
$scope.statuscode=response.status;
$scope.statustext=response.statustext;
$scope.statusheaders=response.headers();
$scope.statusconfig=response.config;
},函数myError(响应){
$scope.myMessage=“错误!”;
$scope.response=响应;
$scope.statuscode=response.status;
$scope.statustext=response.statustext;
$scope.statusheaders=response.headers();
$scope.statusconfig=response.config;
});
$scope.getNext=函数(){
$http({
方法:“获取”,
url:$scope.nextLink
}).然后(函数mySucces(响应){
$scope.myMessage=“成功!”;
$scope.response=响应;
$scope.outsideData=response.data;
$scope.meta=response.data.meta;
$scope.insideData=$scope.insideData.concat(response.data.data);
$scope.links=response.data.links;
$scope.selfLink=response.data.links.self;
$scope.firstLink=response.data.links.first;
$scope.lastLink=response.data.links.last;
$scope.nextLink=response.data.links.next;
$scope.prevLink=response.data.links.prev;
$scope.statuscode=response.status;
$scope.statustext=response.statustext;
$scope.statusheaders=response.headers();
$scope.statusconfig=response.config;
},函数myError(响应){
$scope.myMessage=“错误!”;
$
<style>
* {
box-sizing: border-box;
font-family: sans-serif;
}
h1,
h2{
margin-left:10px;
}
.inner{
display:inline-block;
vertical-align:top;
padding:5px;
position:relative;
}
.inner img{
width:100%;
}
.inner .outer-caption{
color:black;
width:375px;
height:125px;
background-color:#fc7cab;
display:flex;
}
.inner .outer-caption .inner-caption{
font-size:14px;
font-family:sans-serif;
overflow:hidden;
width:320px;
height:70px;
margin:auto;
}
.inner .overlay-color{
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
background-color: #fff;
transition: all .5s;
}
.inner .overlay-text{
border: 2px solid #7e7e7e;
color:#7e7e7e;
font-size: 10px;
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
width:70px;
height:35px;
line-height:35px;
margin:auto;
border-radius:1px;
text-align: center;
transition: all .5s;
</style>
<body>
<h1>{{title}}</h1>
<h2>{{myMessage}}</h2>
<div id="outer" class="outer">
<div class="inner" ng-mouseenter="makeVisibile()" ng-mouseleave="makeInVisibile()" ng-repeat="x in insideData">
<img ng-if="x.service=='Instagram'||(x.service=='Twitter' && x.mediaType=='image')" ng-src='{{x.thumbnails[0].url}}'>
<div ng-if="x.service=='Twitter'&& x.mediaType!='image'" class="outer-caption"><div class="inner-caption">{{x.caption}}</div></div>
<div class="overlay-color" ng-style="overlayColorStyles" ></div>
<div class="overlay-text" ng-style="overlayTextStyles">VIEW</div>
</div>
</div>
<script>
// Create the module
var appModule = angular.module('appName', []);
// Create rootScope variables
appModule.run(
function($rootScope){
$rootScope.title = "Taneleer Demonstration";
}
);
// Create the controller
appModule.controller('appCtrl', function($scope, $http) {
$scope.overlayColorStyles = {"opacity": 0};
$scope.overlayTextStyles = {"opacity": 0};
$scope.makeVisibile = function(){
//alert("test");
//document.getElementById("overlay-color").style.opacity = .75;
//document.getElementById("overlay-text").style.opacity = 1;
$scope.overlayColorStyles = {"opacity" : .75};
$scope.overlayTextStyles = {"opacity" : 1};
}
$scope.makeInVisibile = function(){
//alert("test");
//document.getElementById("overlay-color").style.opacity = 0;
//document.getElementById("overlay-text").style.opacity = 0;
$scope.overlayColorStyles = {"opacity" : 0};
$scope.overlayTextStyles = {"opacity" : 0};
}
$http({
method : "GET",
url : "https://taneleer.composedcreative.com/api/v1/feed/a0329f16-9225-11e6-89bb-296a97b9d609/bb0429f6-f0ca-11e7-8f5d-d92739a9a53f"
}).then(function mySucces(response) {
$scope.myMessage = "Success!";
$scope.response = response;
$scope.meta = response.data.meta;
$scope.outsideData = response.data;
$scope.insideData = response.data.data;
$scope.links = response.data.links;
$scope.selfLink = response.data.links.self;
$scope.firstLink = response.data.links.first;
$scope.lastLink = response.data.links.last;
$scope.nextLink = response.data.links.next;
$scope.prevLink = response.data.links.prev;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
}, function myError(response) {
$scope.myMessage = "Error!";
$scope.response = response;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
});
$scope.getNext = function() {
$http({
method : "GET",
url : $scope.nextLink
}).then(function mySucces(response) {
$scope.myMessage = "Success!";
$scope.response = response;
$scope.outsideData = response.data;
$scope.meta = response.data.meta;
$scope.insideData = $scope.insideData.concat(response.data.data);
$scope.links = response.data.links;
$scope.selfLink = response.data.links.self;
$scope.firstLink = response.data.links.first;
$scope.lastLink = response.data.links.last;
$scope.nextLink = response.data.links.next;
$scope.prevLink = response.data.links.prev;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
}, function myError(response) {
$scope.myMessage = "Error!";
$scope.response = response;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
});
}
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$scope.getNext();
}
});
});
</script>
<style>
* {
box-sizing: border-box;
font-family: sans-serif;
}
h1,
h2{
margin-left:10px;
}
.outer-wrapper{
overflow: hidden;
}
.inner-wrapper{
display:inline-block;
vertical-align:top;
padding:5px;
position:relative;
}
.inner-wrapper img{
width:100%;
}
.inner-wrapper .outer-caption{
color:black;
width:100%;
padding-top:35%;
background-color:#fc7cab;
position:relative;
}
.inner-wrapper .outer-caption .inner-caption{
font-size:14px;
font-family:sans-serif;
overflow:hidden;
width:75%;
height:70px;
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
margin:auto;
}
.inner-wrapper .item-overlay-color{
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
background-color: #fff;
transition: all .5s;
opacity: 0.0;
}
.inner-wrapper:hover .item-overlay-color {
opacity: 0.75;
}
.inner-wrapper .item-overlay-text{
border: 2px solid #7e7e7e;
color:#7e7e7e;
font-size: 10px;
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
width:70px;
height:35px;
line-height:35px;
margin:auto;
border-radius:1px;
text-align: center;
transition: all .5s;
opacity: 0.0;
}
.inner-wrapper:hover .item-overlay-text {
opacity: 1.0;
}
.inner-wrapper .page-overlay {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(255,255,255,0.75);
z-index: 2;
cursor: pointer;
}
.inner-wrapper #page-overlay {
opacity:0;
transition: all .5s;
pointer-events:none;
}
.inner-wrapper .page-overlay .text{
position: absolute;
top: 50%;
left: 50%;
font-size: 50px;
color: white;
transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
}
.inner-wrapper .page-overlay .text .close{
position:absolute;
top:0;
right:0;
color:#000;
font-weight: lighter;
font-size: 30px;
line-height: 30px;
padding-top:5px;
padding-right:5px;
}
@media screen and (min-width: 1301px){
.inner-wrapper{
width:16.6666%;
}
}
@media screen and (max-width: 1300px){
.inner-wrapper{
width:20%;
}
}
@media screen and (max-width: 1024px){
.inner-wrapper{
width:25%;
}
}
@media screen and (max-width: 768px){
.inner-wrapper{
width:50%;
}
}
@media screen and (max-width: 480px){
.inner-wrapper{
width:100%;
}
}
</style>
<body>
<div id="outer-wrapper" class="outer-wrapper">
<div id="inner-wrapper" class="inner-wrapper" ng-repeat="x in insideData">
<img ng-if="x.service=='Instagram'||(x.service=='Twitter' && x.mediaType=='image')" ng-src='{{x.thumbnails[0].url}}'>
<div class="outer-caption" ng-if="x.service=='Twitter'&& x.mediaType!='image'">
<div class="inner-caption">{{x.caption}}</div>
</div>
<div class="item-overlay-color"></div>
<div class="item-overlay-text" ng-click="showOverlay()">VIEW</div>
<div id="page-overlay" class="page-overlay">
<div class="text">
<img ng-src='{{x.mainImage.url}}'>
<span class="close" ng-click="hideOverlay()">X</span>
</div>
</div>
</div>
</div>
</body>
<script>
// Create the module
var appModule = angular.module('appName', []);
// Create rootScope variables
appModule.run(
function($rootScope){
$rootScope.title = "Taneleer Demonstration";
}
);
// Create the controller
appModule.controller('appCtrl', function($scope, $http) {
$scope.showOverlay = function(){
document.getElementById("page-overlay").style.opacity = 1;
document.getElementById("page-overlay").style["pointer-events"] = "auto";
}
$scope.hideOverlay = function(){
document.getElementById("page-overlay").style.opacity = 0;
document.getElementById("page-overlay").style["pointer-events"] = "none";
}
$http({
method : "GET",
url : "https://taneleer.composedcreative.com/api/v1/feed/a0329f16-9225-11e6-89bb-296a97b9d609/bb0429f6-f0ca-11e7-8f5d-d92739a9a53f"
}).then(function mySucces(response) {
$scope.myMessage = "Success!";
$scope.response = response;
$scope.meta = response.data.meta;
$scope.outsideData = response.data;
$scope.insideData = response.data.data;
$scope.links = response.data.links;
$scope.selfLink = response.data.links.self;
$scope.firstLink = response.data.links.first;
$scope.lastLink = response.data.links.last;
$scope.nextLink = response.data.links.next;
$scope.prevLink = response.data.links.prev;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
}, function myError(response) {
$scope.myMessage = "Error!";
$scope.response = response;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
});
$scope.getNext = function() {
$http({
method : "GET",
url : $scope.nextLink
}).then(function mySucces(response) {
$scope.myMessage = "Success!";
$scope.response = response;
$scope.outsideData = response.data;
$scope.meta = response.data.meta;
$scope.insideData = $scope.insideData.concat(response.data.data);
$scope.links = response.data.links;
$scope.selfLink = response.data.links.self;
$scope.firstLink = response.data.links.first;
$scope.lastLink = response.data.links.last;
$scope.nextLink = response.data.links.next;
$scope.prevLink = response.data.links.prev;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
}, function myError(response) {
$scope.myMessage = "Error!";
$scope.response = response;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
$scope.statusheaders = response.headers();
$scope.statusconfig = response.config;
});
}
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$scope.getNext();
}
});
});
app.controller('exampleController', function($scope) {
$scope.overlayColorStyles = {"opacity": 0};
$scope.overlayTextStyles = {"opacity": 0};
$scope.overlayTextStylesActive = {"opacity": 1};
$scope.overlayColorStylesActive = { "opacity": .75};
$scope.activeItemIndex = null;
});
<div id="outer" class="outer">
<div class="inner"
ng-mouseenter="$scope.activeItemIndex = $index"
ng-mouseleave="$scope.activeItemIndex = null"
ng-repeat="x in insideData">
<img ng-if="x.service=='Instagram'||(x.service=='Twitter' && x.mediaType=='image')" ng-src='{{x.thumbnails[0].url}}'>
<div ng-if="x.service=='Twitter'&& x.mediaType!='image'" class="outer-caption"><div class="inner-caption">{{x.caption}}</div></div>
<div class="overlay-color"
ng-style="$scope.activeItemIndex === $index ? overlayColorStylesActive : overlayColorStyles">
</div>
<div class="overlay-text"
ng-style="$scope.activeItemIndex === $index ? overlayTextStylesActive : overlayTextStyles">
VIEW
</div>
</div>
</div>
<div id="outer" class="outer">
<div class="inner"
ng-repeat="x in insideData">
<img ng-if="x.service=='Instagram'||(x.service=='Twitter' && x.mediaType=='image')" ng-src='{{x.thumbnails[0].url}}'>
<div ng-if="x.service=='Twitter'&& x.mediaType!='image'" class="outer-caption"><div class="inner-caption">{{x.caption}}</div></div>
<div class="overlay-color"></div>
<div class="overlay-text">VIEW</div>
</div>
</div>
.overlay-color {
opacity: 0.0;
}
.overlay-text {
opacity: 0.0;
}
.inner:hover .overlay-text {
opacity: 1.0;
}
.inner:hover .overlay-color {
opacity: 0.75;
}