html - 碰到标志 Div 时缩放 SVG
问题描述
我们有一个 SVG 波浪设计,出现在 WordPress 网站的导航下方。SVG 目前是浮动的,并且希望 SVG 在到达徽标所在的 div 时开始随着浏览器缩小。本质上,我们希望 SVG 覆盖导航中的所有内容,但从不覆盖徽标,即使在缩小。这甚至可能吗?
有关实时示例和代码,请参见http://dev-wisco-radio.pantheonsite.io/
解决方案
除了使用百分比宽度或针对屏幕尺寸变化的媒体查询来近似效果之外,我无法使用纯 CSS 找到解决方案。然而,这完全可以通过 Javascript 根据屏幕和徽标宽度调整 SVG 大小来实现。这是基于您当前的 HTML 和 CSS 使用 JS 的快速实现:
var wave = document.getElementsByClassName("homepage-svg")[0];
var logo = document.getElementsByClassName("site-branding")[0];
window.addEventListener('resize', function(event){
var screenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; //Support for different browsers
var logoWidth = logo.offsetWidth;
var maxSVGWidth = 1200; //1200px was obtained from your CSS
//If the screen is too small to show both the wave and the logo at full size (with no overlap), decrease the width of the wave
if(screenWidth < maxSVGWidth + logoWidth) {
wave.style.width = screenWidth - logoWidth + "px";
}
});
我在您共享的链接(在 Chrome 中)上对此进行了测试,它可以正常工作,但是非常粗糙并且不考虑初始页面加载,因为它仅在调整窗口大小时运行。但是,进行任何必要的调整应该相当容易。
希望这可以帮助!:)
推荐阅读
- unit-testing - Hot to Mock 连接中使用的 DbQuery
- coq - 在 Coq 中对两个子目标使用相同的证明
- android - 使用 ARC Welder 抓取移动 Android 应用程序?
- javascript - 获取 React 状态(对象的对象(内部示例))并将对象数组获取到 map() 以进行组件渲染
- express - 使用本机反应将图像上传到heroku
- angularjs - ng-click HTML 代码工作正常,但 angularjs 函数中的相同代码。ng-click 不起作用
- c# - 是否可以使用 gRPC 从服务器到客户端执行 RPC 请求?
- c# - 分离控制器中的 ASP.NET Core userManager 和上下文
- arrays - 如何在 SUMPRODUCT 中使用带有命名范围的 COUNTIF 和另一个过滤器?
- java - HTTP 状态 415 - Spring MVC 中不支持的媒体类型错误与 Angular js