基于jQuery-Mobile对话框实现的简单相册

本文介绍了使用jQuery-Mobile创建对话框式相册的两种方法。第一种方法通过手动编写HTML和CSS,设置每个图片链接和弹出窗口。第二种方法利用JavaScript循环,从图片数组中动态生成链接和弹出窗口,简化了开发过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    博主在这里介绍两种方法,

一、第一种方法(与网上的大部分相似,较为麻烦)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../css/jquery.mobile-1.4.5.min.css"/>
    <script src="../js/jquery.min.js"></script>
	<script src="../js/jquery.mobile-1.4.5.min.js" ></script>
	</head>
	<body>
		<div data-role="page">
		<!-- data-rel="popup"——设置当前元素具有弹出窗的功能;
		data-role="popup"——设置当前元素为弹出窗;-->
			<a href="#popup1_1" data-rel="popup" data_position-to="window">
				<img src="../img/a1.jpg" style="width:49%"></img>
				</a>
			<a href="#popup1_2" data-rel="popup" data_position-to="window">
				<img src="../img/a2.jpg" style="width:49%"></img>
				</a>
			<a href="#popup1_3" data-rel="popup" data_position-to="window">
				<img src="../img/a3.jpg" style="width:49%"></img>
				</a>
			<a href="#popup1_4" data-rel="popup" data_position-to="window">
				<img src="../img/a4.jpg" style="width:49%"></img>
				</a>
			<a href="#popup1_5" data-rel="popup" data_position-to="window">
				<img src="../img/a5.jpg" style="width:49%"></img>
				</a>
			<a href="#popup1_1" data-rel="popup" data_position-to="window">
				<img src="../img/a6.jpg" style="width:49%"></img>
				</a>
				
				<div data-role="popup" id="popup1">
					<a href="#" data-rel="back" data-role="button" data-icon="delete"
						data-iconpos="notetext" class="ui-btn-right">Close</a>
						<img src="../img/a1.jpg" style="max-height: 500px;">
				</div>
				<div data-role="popup" id="popup2">
					<a href="#" data-rel="back" data-role="button" data-icon="delete"
						data-iconpos="notetext" class="ui-btn-right">Close</a>
						<img src="../img/a2.jpg" style="max-height: 500px;">
				</div>
				<div data-role="popup" id="popup3">
					<a href="#" data-rel="back" data-role="button" data-icon="delete"
						data-iconpos="notetext" class="ui-btn-right">Close</a>
						<img src="../img/a3.jpg" style="max-height: 500px;">
				</div>
				<div data-role="popup" id="popup4">
					<a href="#" data-rel="back" data-role="button" data-icon="delete"
						data-iconpos="notetext" class="ui-btn-right">Close</a>
						<img src="../img/a4.jpg" style="max-height: 500px;">
				</div>
				<div data-role="popup" id="popup5">
					<a href="#" data-rel="back" data-role="button" data-icon="delete"
						data-iconpos="notetext" class="ui-btn-right">Close</a>
						<img src="../img/a5.jpg" style="max-height: 500px;">
				</div>
				<div data-role="popup" id="popup6">
					<a href="#" data-rel="back" data-role="button" data-icon="delete"
						data-iconpos="notetext" class="ui-btn-right">Close</a>
						<img src="../img/a6.jpg" style="max-height: 500px;">
				</div>
		</div>
	</body>
</html>

二、第二种方法(将图片或图片地址放进一个数组,循环)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<link rel="stylesheet" type="text/css" 
    			href="../css/jquery.mobile-1.4.5.min.css">
    	<script src="../js/jquery.min.js"></script>
		<script src="../js/jquery.mobile-1.4.5.min.js" ></script>
		<style>
			.ui-btn {
				display:inline-block;
			}
		</style>
	</head>
	<body>
		<div data-role="page" > 
			
		</div>
		<script>	
				var imgList = ["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg"];
				var index =0;
				for (index=0;index < imgList.length;index++) {
					var linkx="<a href='#pop"+ index+"' data-rel='popup'"
							+ "data-role='button' data-position-to='window' style='width:40%;' >"
							+ "<img src='"+ imgList[index] +"' style='width:75%;max-height:70px;' >"
							+ "</a>";
					
					var popupx="<div data-role='popup' id='pop"+ index +"'>"
							+ "   <a href='#' data-rel='back' data-role='button' data-icon='delete' "
							+ "  data-icon='delete' data-iconpos='notext' class='ui-btn-right'>close</a> "
							+ "  <img src='"+ imgList[index] +"' style='max-height:512px;'>"
							+"</div>"
					
					//var x= '<img src="../img/' + imgList[index] + '" alt="图片不能正常显示">'; 
					//var y= "<img src='../img/"+ imgList[index] + "' alt='图片不能正常显示'>";
					
					$("[data-role='page']").append(linkx);
					$("[data-role='page']").append(popupx);
				}

		</script>
	</body>
</html>

    图片的地址自己改动哈,此方法的难点在于字符串的拼接问题,需要重点理解

最后的效果图:(样式没有调整,有点丑陋,请忽略)
在这里插入图片描述
在这里插入图片描述
    以上就是使用jQuery-mobile实现对话框的简单相册,希望能对大家有所帮助。若有任何疑问或是不解请在下方评论,谢谢。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值