首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当按钮点击javascript事件改变字体大小时,可以重新评估Art Direction吗?

问当按钮点击javascript事件改变字体大小时,可以重新评估Art Direction吗?
EN

Stack Overflow用户
提问于 2019-07-14 17:27:12
回答 1查看 40关注 0票数 0

我的艺术方向和响应式图像代码可以工作(甚至以em为单位),但当html元素的字体大小发生变化时,它似乎不会重新评估(即。当em的大小发生变化时。)我想增加字体大小,以使图像使用更高质量的版本,如果值得的话,和/或使用艺术导向(如果这是一个术语)版本。zoom-in;),如果字体减小,则在适当的情况下缩小。

我在源html元素的媒体和大小属性中切换为em单位。我使用calc来减去1个像素,而不是使用十进制数的em单位。

我创建了一个jsfiddle来组合html、css和相关的javascript,如下所示:https://jsfiddle.net/gt6omaez/

代码语言:javascript
复制
HTML
<div class=testing-area><!--
     --><a
            class=yard-sign
            title="Click to visit Campaign Page"
            rel="noopener noreferrer"
            target="_blank"
            href="https://howiehawkins.us"
        ><!--
         --><picture><!--
             --><source
                    type="image/png"
                    media="(max-width:calc(20em-1px))"
                    srcset="https://www.stevemario.com/images/howie-hawkins-2020-logo.png 110w,
                        https://www.stevemario.com/images/howie-hawkins-2020-logo-146.png 146w,
                        https://www.stevemario.com/images/howie-hawkins-2020-logo-175.png 175w,
                        https://www.stevemario.com/images/howie-hawkins-2020-logo-218.png 218w
                    "
                    sizes="6.875em"
                ><!--
             --><source
                    type="image/png"
                    media="(min-width:20em)"
                    srcset="https://www.stevemario.com/images/howie-hawkins-2020.png 290w,
                        https://www.stevemario.com/images/howie-hawkins-2020-386.png 386w,
                        https://www.stevemario.com/images/howie-hawkins-2020-463.png 463w,
                        https://www.stevemario.com/images/howie-hawkins-2020-579.png 579w
                    "
                    sizes="18.125em"
                ><!--
             --><img
                    alt="Howie Hawkins 2020 campaign logo"
                    src="https://www.stevemario.com/images/howie-hawkins-2020-logo.png"
                    srcset="https://www.stevemario.com/images/howie-hawkins-2020-logo.png 110w,
                        https://www.stevemario.com/images/howie-hawkins-2020-logo-146.png 146w,
                        https://www.stevemario.com/images/howie-hawkins-2020-logo-175.png 175w,
                        https://www.stevemario.com/images/howie-hawkins-2020-logo-218.png 218w
                    "
                    sizes="6.875em"
                ><!--
         --></picture><!--
     --></a><!--
     --><div class=font-resize-buttons-area><!--
         --><span>Font Size:</span><!--
         --><a
                class=font_increase_button
                title="Click to increase font size"
            ><!--
            -->+<!--
         --></a><!--
         --><a
                class=font_decrease_button
                title="Click to decrease font size"
            ><!--
             -->-<!--
         --></a><!--
     --></div><!--
 --></div>

Javascript
document.documentElement.style.fontSize='12pt';function increase_font_size(){document.documentElement.style.fontSize=parseFloat(document.documentElement.style.fontSize)+0.5+'pt';}function decrease_font_size(){document.documentElement.style.fontSize=parseFloat(document.documentElement.style.fontSize)-0.5+'pt';}document.addEventListener('DOMContentLoaded',function(){var font_increase_buttons=document.getElementsByClassName('font_increase_button');for(const font_increase_button of font_increase_buttons){font_increase_button.addEventListener('click',function(){increase_font_size();});}var font_decrease_buttons=document.getElementsByClassName('font_decrease_button');for(const font_decrease_button of font_decrease_buttons){font_decrease_button.addEventListener('click',function(){decrease_font_size();});}});

CSS
body
{
margin:0;
background-color:#00AE00
}
.testing-area
{
padding:0.5em;
box-sizing:border-box;
max-width:320px;
background-color:black;
margin:0 auto;
line-height:1
}
.yard-sign
{
display:inline-block;
padding:0.21875em 0.4375em
}
.yard-sign:hover
{
background-color:#00AE00;
color:black
}
.yard-sign:hover picture
{
filter:invert(1)
}
.yard-sign img
{
height:2.8125em;
vertical-align:top
}
.font-resize-buttons-area
{
display:inline-block;
font-size:3em
}
.font-resize-buttons-area > *
{
display:inline-block;
box-sizing:border-box;
min-width:1em;
min-height:1em
}
.font-resize-buttons-area > span
{
color:white
}
.font-resize-buttons-area > a
{
color:#00AE00;
text-align:center
}
.font-resize-buttons-area > a:hover
{
background-color:#00AE00;
color:black
}

有一个指向活动网页的<a>标签链接,它包含了一张图片。该图像是活动标志的图像。该图像使用<picture>标签,包含2个具有media属性(用于艺术方向)的<source>标签的子级,以及一个用于不支持它的浏览器的<img>标签。它们中的每一个都有srcset属性和sizes,都是以em为单位的。<img>还设置了alt属性和src属性。

然后,还有增大和减小字体大小的按钮。有一个javascript可以将点击事件添加到这些事件中,从而完成这些操作。

有一个css用来显示所使用的徽标,但也用来演示问题。我使用了一个max-width样式为320px的容器,这样就可以测试一个临界阈值。320px是一个完整的标志将被显示的阈值,即。使用描述符。小于320px将显示不完整的徽标,即没有描述符。如果您移动jsfiddle的条,并调整框架宽度,您可以看到艺术方向正在工作(或者至少我希望它对您有效,而不仅仅是我)。

容器将允许您通过单击按钮将图像保留为320px的可识别区域(背景为黑色),以便在您增加字体大小时使用。

当字体变大时,我看到完整的徽标仍然存在,并溢出容器,而不是有一些艺术方向重新评估,以显示不完整的徽标。

减小字体大小似乎也不会导致重新评估艺术方向,因为将帧宽度降低到320px以下,然后多次减小字体大小并不会显示完整的徽标。

EN

回答 1

Stack Overflow用户

发布于 2019-07-15 14:03:35

我尝试在<source>元素的media属性中使用css变量,但也没有成功。我已经编写了更改media和sizes属性的javascript代码,它可以按我所希望的那样工作。对于感兴趣的人:https://jsfiddle.net/u7arm0cf/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57026130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档