如何在点击的确切卡片上弹出消息?
How to pop-up message on the exact card where it's clicked?
我正在尝试显示卡片列表中的一些元素,当我点击一张卡片时,我需要在该卡片上显示一条弹出消息。现在它显示在最上面的卡片上。但我需要它显示在我点击的卡片上。
这是我的 html 代码,用于显示卡片元素和弹出窗口:
<a class="external" ng-repeat="card in cardData" ng-click="popUpFunction()">
<div class="card">
<img class="card-image" src="{{card.thumbNail_image}}" alt="">
<div class="popup">
<span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>
<div class="card-infos">
<h2 class="card-title">{{card.contentDescription}}</h2>
</div>
</div>
</a>
这是 css 代码:
.popup {
position: relative;
display: inline-block;
cursor: pointer;
height:0px;
width:0px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
margin-left: 80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
这是 js 代码:
$scope.popUpFunction = function () {
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
您可以单击元素并将事件传递给函数。在事件对象中,您可以提取点击元素的目标 属性。这是一个快速演示来说明。
此外,您的点击似乎是一个循环,我不知道 angularJS 但我认为应该是这样的:
<div class="card" ng-click="popUpFunction($event)"> // notice the $event
而不是
<a class="external" ng-repeat="card in cardData" ng-click="popUpFunction()">
function onClickShowMessage(event) {
const card = event.target;
const message = createMessageElement();
card.appendChild(message);
}
function createMessageElement() {
const message = document.createElement('p');
message.innerHTML = 'This is some message';
message.classList.add('message');
return message;
}
.card {
width: 200px;
min-height: 150px;
background-color: yellow;
cursor: pointer;
margin-bottom: 5px;
display: block;
}
.message {
background-color: firebrick;
color: white;
padding: 1rem;
}
<a class="card" onclick="onClickShowMessage(event)"></a>
<a class="card" onclick="onClickShowMessage(event)"></a>
<a class="card" onclick="onClickShowMessage(event)"></a>
为了解决这个问题,我将卡的索引作为参数传递并在ng-if
中进行了检查
<a class="external" ng-repeat="card in cardData" ng-click="popUpFunction()">
<div class="card">
<img class="card-image" src="{{card.thumbNail_image}}" alt="">
<div class="popup">
<span class="popuptext" id="myPopup" ng-if="popUpIndex==$index">A Simple Popup!</span>
</div>
<div class="card-infos">
<h2 class="card-title">{{card.contentDescription}}</h2>
</div>
</div>
</a>
这是脚本文件中的更改:
$scope.popUpIndex = 0;
$scope.modalFunction = function (ind) {
console.log(ind);
$scope.popUpIndex = ind;
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
我正在尝试显示卡片列表中的一些元素,当我点击一张卡片时,我需要在该卡片上显示一条弹出消息。现在它显示在最上面的卡片上。但我需要它显示在我点击的卡片上。
这是我的 html 代码,用于显示卡片元素和弹出窗口:
<a class="external" ng-repeat="card in cardData" ng-click="popUpFunction()">
<div class="card">
<img class="card-image" src="{{card.thumbNail_image}}" alt="">
<div class="popup">
<span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>
<div class="card-infos">
<h2 class="card-title">{{card.contentDescription}}</h2>
</div>
</div>
</a>
这是 css 代码:
.popup {
position: relative;
display: inline-block;
cursor: pointer;
height:0px;
width:0px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
margin-left: 80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
这是 js 代码:
$scope.popUpFunction = function () {
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
您可以单击元素并将事件传递给函数。在事件对象中,您可以提取点击元素的目标 属性。这是一个快速演示来说明。
此外,您的点击似乎是一个循环,我不知道 angularJS 但我认为应该是这样的:
<div class="card" ng-click="popUpFunction($event)"> // notice the $event
而不是
<a class="external" ng-repeat="card in cardData" ng-click="popUpFunction()">
function onClickShowMessage(event) {
const card = event.target;
const message = createMessageElement();
card.appendChild(message);
}
function createMessageElement() {
const message = document.createElement('p');
message.innerHTML = 'This is some message';
message.classList.add('message');
return message;
}
.card {
width: 200px;
min-height: 150px;
background-color: yellow;
cursor: pointer;
margin-bottom: 5px;
display: block;
}
.message {
background-color: firebrick;
color: white;
padding: 1rem;
}
<a class="card" onclick="onClickShowMessage(event)"></a>
<a class="card" onclick="onClickShowMessage(event)"></a>
<a class="card" onclick="onClickShowMessage(event)"></a>
为了解决这个问题,我将卡的索引作为参数传递并在ng-if
<a class="external" ng-repeat="card in cardData" ng-click="popUpFunction()">
<div class="card">
<img class="card-image" src="{{card.thumbNail_image}}" alt="">
<div class="popup">
<span class="popuptext" id="myPopup" ng-if="popUpIndex==$index">A Simple Popup!</span>
</div>
<div class="card-infos">
<h2 class="card-title">{{card.contentDescription}}</h2>
</div>
</div>
</a>
这是脚本文件中的更改:
$scope.popUpIndex = 0;
$scope.modalFunction = function (ind) {
console.log(ind);
$scope.popUpIndex = ind;
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}