gpt4 book ai didi

javascript - 如何在动态 div 中以大尺寸 onmouseover 显示图像?

转载 作者:太空狗 更新时间:2023-10-29 14:54:53 25 4
gpt4 key购买 nike

我正在尝试用 angularJS 制作一个简单的照片库。下面是代码

index.html

<!DOCTYPE html>
<html >
<head>
<title></title>

<script src="http://code.angularjs.org/1.2.0rc1/angular.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="test.js"></script>

</head>
<body ng-app="testModule" ng-controller="testCtrl">
<div style="width: 60%; margin: 0 auto;">
<div id="dp"></div>
</div>
</body>
</html>

test.js( Controller )

function testMe(imgSrc) {
alert(imgSrc);
}

angular
.module('testModule', [])
.controller('testCtrl', function($scope) {
var photoSource = [
["images/ph1.jpg", "images/ph2.jpg"],
["images/ph5.jpg", "images/ph6.jpg"]
];
var body = "<table>";
var row = 2;
var col = 2;
for (var i = 0; i < row; i++) {
body += "<tr>";
for (var j = 0; j < col; j++) {
body += "<td> <img id='" + i + j + "' src='" + photoSource[i][j] + "' onmouseover=testMe('" + photoSource[i][j] + "');></td>";
}
body += "</tr>";
}
body += "</table>";
console.log(body);
$("#dp").html(body);
});

问题是,当鼠标悬停在图像上时,我想将该图像显示在 div 标记的中央。但是这部分我无法实现。

最佳答案

您不需要在 Controller 中操作 html,而是使用 Angular 绑定(bind),这样您的 javascript 就变成了

function testMe(imgSrc) {

alert(imgSrc);
}

angular
.module('testModule', [])
.controller('testCtrl', function ($scope) {

$scope.photoSource = [
[ "images/ph1.jpg","images/ph2.jpg"],
[ "images/ph5.jpg","images/ph6.jpg"]
];

$scope.showFullImage = function(photoSrc) {
// this function will call when you mouseover so add logic here and photosrc will be current mouseover image src
}
});

现在在你的 html 中使用这个 photoSource 范围变量来生成表格

<body ng-app="testModule" ng-controller="testCtrl">
<div style="width: 60%; margin: 0 auto;">
<div id="dp">
<table>
<tr ng-repeat="photos in photoSource">
<td ng-repeat="photo in photos">
<img ng-src="{{photo}}" ng-mouseover="showFullImage(photo)" />
</td>
</tr>
</table>
</div>
</div>
</body>

关于javascript - 如何在动态 div 中以大尺寸 onmouseover 显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com