html div内容纵向对齐,html – 使用transform进行垂直对齐:translateY(-50%);里面绝对定位的div...

本文探讨了一种使H2元素在不同浏览器中垂直居中的方法,并针对SafariIOS中的显示问题提供了可能的解决方案。作者尝试了使用translateY来实现垂直居中,但在SafariIOS上遇到了元素消失的问题。

我正在尝试使用以下代码将h2垂直居中在div内,该div绝对定位高度为50%:

#owl-demo h2 {

position: relative;

top: 50%;

-webkit-transform: translateY(-50%);

-ms-transform: translateY(-50%);

transform: translateY(-50%);

}

在Chrome上正常工作,但是在Safari IOS中h2消失,除非我将h2设置为绝对位置,然后我无法使用text-align将文本居中.

任何帮助非常感谢,我一直在努力使这项工作整天.

编辑:所以看起来高度50%是父div的问题,如果我删除它,文本出现在IOS中.

我正在尝试实现覆盖50%缩略图高度的叠加层是否有另一种方法可以覆盖50%的缩略图?

#owl-demo .thumb-overlay {

text-align: center;

height: 50%;

width: 100%;

position: absolute;

left: 0;

bottom: 0;

background:rgba(0,.8);

z-index: 9999;

opacity:0;

filter: alpha(opacity = 0);

-webkit-transition: all 300ms ease-out;

-moz-transition: all 300ms ease-out;

-o-transition: all 300ms ease-out;

-ms-transition: all 300ms ease-out;

transition: all 300ms ease-out;

}

#owl-demo .thumb-overlay:hover {

opacity:0.75;

filter: alpha(opacity = 75);

transition:opacity 0.25s;

-moz-transition:opacity 0.25s;

-webkit-transition:opacity 0.25s;

}

.touch #owl-demo .thumb-overlay,{

opacity:1;

filter: alpha(opacity = 1);

}

#owl-demo h2 {

font-size: .875em;

position: relative;

top: 50%;

-webkit-transform: translateY(-50%);

-ms-transform: translateY(-50%);

transform: translateY(-50%);

color: $white;

}

import base64 import os import shutil from bs4 import BeautifulSoup import tqdm import webbrowser # 配置参数 root_folder = r"C:\Users\admin\Desktop\out1\out1" # 替换为你的文件夹路径 output_html = os.path.join(root_folder, "output.html") # 将 HTML 保存到 root_folder 下 prefixes = ['C2', 'C3', 'E', 'G'] suffixes = ['MP1.png', 'MP2.png', 'MP3.png', 'MPC.png'] # 创建用于存放图片的子文件夹 image_output_dir = os.path.join(root_folder, "images") os.makedirs(image_output_dir, exist_ok=True) # 提取所有“标识符-序号” identifiers_and_indices = [] for folder in os.listdir(root_folder): if not os.path.isdir(os.path.join(root_folder, folder)): continue parts = folder.split('_') if len(parts) < 1: continue identifier_index_part = parts[0] if '-' not in identifier_index_part: continue identifier, index = identifier_index_part.split('-') identifiers_and_indices.append((identifier, int(index))) # 去重并排序 unique_identifier_index = sorted(set(identifiers_and_indices), key=lambda x: (x[1], x[0])) all_identifiers = sorted(set([id for id, _ in unique_identifier_index])) all_indices = sorted(set([idx for _, idx in unique_identifier_index])) txt = "".join([f'<li><a href="#power-{index}" data-power="{index}">第{index}次测试</li></a>' for index in all_indices]) # 构建 HTML 表格和 JavaScript 图片放大功能 html_content = """ <html> <head> <title>图像表格</title> <style> body { font-family: Arial, sans-serif; } table { border-collapse: collapse; margin-bottom: 40px; } td, th { border: 1px solid #ccc; padding: 5px; text-align: center; } img { width: 90%; height: auto; cursor: pointer; transition: transform 0.2s ease-in-out; loading: lazy; } img:hover { transform: scale(1.1); } /* 图片弹窗样式 */ .modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.9); animation: fadeIn 0.15s; } .modal-info { color: white; margin-top: 15px; font-size: 1.1rem; text-align: center; } .modal-content { position: relative; margin: auto; padding: 20px; max-width: 60%; max-height: 60%; display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: auto; } .modal-image { max-width: 60%; max-height: 60%; border: 2px solid white; border-radius: 5px; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5); transform:center; } .close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; } .close:hover, .close:focus { color: #bbb; text-decoration: none; cursor: pointer; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); font-size: 80px; color: white; cursor: pointer; } .prev { left: 200px; } .next { right: 200px; } .prev:hover, .next:hover { color: #ccc; } .sidebar { position: fixed; /* 固定定位 */ top: 0; /* 距离顶部 0 */ left: 0; /* 左边对齐 */ width: 100%; height:20px background-color: #333; color: white; padding: 15px 10px; z-index: 1000; /* 确保在其他内容之上 */ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2); } .sidebar h2 { margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid #ddd; font-size: 1.2rem; } .nav-links { list-style: none; display: flex; /* 使用 Flex 布局 */ flex-direction: row; /* 横向排列(可省略,默认就是 row) */ gap: 10px; /* 可选:设置导航项之间的间距 */ } .nav-links li { margin-bottom: 8px; } .nav-links a { display: inline-block; padding: 8px 12px; color: var(--dark-color); text-decoration: none; border-radius: 4px; transition: all 0.3s; font-size: 0.9rem; } .nav-links a:hover { background: var(--primary-color); color: white; } .nav-links a.active { background: var(--primary-color); color: white; } .content { margin-top: 50px; /* 根据导航栏高度调整 */ padding: 20px; } </style> </head> <body> <div class='content'> <div class="sidebar"> <ul class="nav-links"> """ + txt + """ </ul> </div> </div> <div id="imageModal" class="modal"> <div> <span class="close" onclick="document.getElementById('imageModal').style.display='none'">×</span> <span class="prev" onclick="prevImage()">❮</span> <span class="next" onclick="nextImage()">❯</span> </div> <div class="modal-content"> <img class="modal-image" id="img01"> <div class="modal-info" id="modalInfo"></div> </div> </div> """ # 存储图片路径和文件名 image_infos = [] for index in tqdm.tqdm(all_indices, desc="处理序号"): html_content += f"<h2 id=power-{index}>第{index}次测试</h2>\n<table border='1'>\n" html_content += "<tr><th>图片名</th>" + "".join([f"<th>{id}</th>" for id in all_identifiers]) + "</tr>\n" for image_name in prefixes: for suffix in suffixes: full_image_name = f"{image_name}_{suffix}" row = f"<tr><td>{full_image_name.split('.')[0]}</td>" for identifier in all_identifiers: folder_path = os.path.join(root_folder, f"{identifier}-{index}_20251118_obsqc") image_path = os.path.join(folder_path, full_image_name) if os.path.exists(image_path): # 复制图片到 images 文件夹,并记录相对路径 new_filename = f"{identifier}-{index}_{full_image_name}" new_image_path = os.path.join(image_output_dir, new_filename) shutil.copy2(image_path, new_image_path) relative_path = os.path.join("images", new_filename).replace("\\", "/") image_infos.append({ 'filename': new_filename, 'path': relative_path }) row += f'<td><img src="{relative_path}" onclick="openModal(\'{relative_path}\')" width="400" height="400"><p class="image-info">{new_filename}</p></td>' else: row += "<td></td>" row += "</tr>\n" html_content += row html_content += "</table>\n<br/>\n" html_content += """ <script> // 存储所有图片的路径和文件名 var imageList = []; window.onload = function() { // 将 Python 提供的 image_infos 转换为 JS 可用的格式 imageList = """ + str(image_infos) + """; } function openModal(imgSrc) { var modal = document.getElementById("imageModal"); var modalImg = document.getElementById("img01"); var modalInfo = document.getElementById("modalInfo"); if (!modal || !modalImg || !modalInfo) { console.error("找不到模态框或相关信息元素,请检查 HTML 结构!"); return; } modal.style.display = "block"; modalImg.src = imgSrc; // 找到当前图片在数组中的位置 window.currentImageIndex = imageList.findIndex(item => item.path === imgSrc); console.log(imageList[window.currentImageIndex]) modalInfo.textContent = imageList[window.currentImageIndex].filename; } // 上一张图片 function prevImage() { if (window.currentImageIndex > 0) { window.currentImageIndex--; var currentImage = imageList[window.currentImageIndex]; document.getElementById("img01").src = currentImage.path; document.getElementById("modalInfo").textContent = currentImage.filename; } } // 下一张图片 function nextImage() { if (window.currentImageIndex < imageList.length - 1) { window.currentImageIndex++; var currentImage = imageList[window.currentImageIndex]; document.getElementById("img01").src = currentImage.path; document.getElementById("modalInfo").textContent = currentImage.filename; } } // 关闭模态框 window.onclick = function(event) { var modal = document.getElementById("imageModal"); if (event.target == modal) { modal.style.display = "none"; } } document.onkeydown = function(e) { if (e.key === "Escape") { document.getElementById("imageModal").style.display = "none"; } else if (e.key === "ArrowLeft") { prevImage(); } else if (e.key === "ArrowRight") { nextImage(); } } </script> </body> </html> """ # 写入 HTML 文件 with open(output_html, "w", encoding="utf-8") as f: f.write(html_content) print(f"HTML 文件已生成:{output_html}") webbrowser.open(output_html) 帮我优化修改导航栏
最新发布
11-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值