【发布时间】:2021-12-13 00:27:28
【问题描述】:
我正在使用这个 JS 和 CSS 为标题图像添加缩放效果。它在桌面上完美运行。
问题在于移动英雄图像没有“填充”整个容器。
所以我需要有 2 个不同的 JS 和 2 个不同的 % 设置,以使其在桌面和移动设备上都可以工作。
是否有一种解决方案可以根据屏幕尺寸制作一个满足我的 2 个不同需求的脚本? “此功能适用于桌面,此功能适用于移动设备”?
对于那些知道我正在使用 GeneratePress 的 Elements 功能来显示整个网站的英雄标题的人。
这是适用于桌面的 JS + CSS:
JS
<script>
var pagehero = document.querySelector('.page-hero');
function hero_animation(){
pagehero.style.backgroundSize = 100+'%';
pagehero.style.opacity = '1';
}
document.onload = hero_animation();
</script>
CSS
.page-hero {
transition: background-size 1s cubic-bezier(0.1, 0.135, 0.15, 0.86), opacity 1s ease-out 0.1s !important;
opacity: 0; background-size: 150% auto;
}
这是我需要为移动设备添加的 JS + CSS:
JS
<script>
var pagehero = document.querySelector('.page-hero');
function hero_animation(){
pagehero.style.backgroundSize = 200+'%';
pagehero.style.opacity = '1';
}
document.onload = hero_animation();
</script>
CSS
.page-hero {
transition: background-size 1s cubic-bezier(0.1, 0.135, 0.15, 0.86), opacity 1s ease-out 0.1s !important;
opacity: 0; background-size: 300% auto;
}
感谢您的帮助!
【问题讨论】:
标签: javascript css zooming css-transitions background-size