首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据方向从大众切换到vh

根据方向从大众切换到vh
EN

Stack Overflow用户
提问于 2017-12-20 11:39:27
回答 1查看 1.6K关注 0票数 0

我有这样的功能,从像素到大众,它非常适合人像定位。

代码语言:javascript
复制
@function get-vw($target) { 
  $vw-context: (320*.01) * 1px;
  @return ($target/$vw-context) * 1vw;
}

但对景观导向来说却很糟糕。很遗憾,但是这样的功能不起作用

代码语言:javascript
复制
@function get-vw($target) { 
  $vw-context: (320*.01) * 1px;
  @media only screen and (orientation: landscape) {
    @return ($target/$vw-context) * 1vh;
  }
  @media only screen and (orientation: portrait) {
    @return ($target/$vw-context) * 1vw;
  }
}

有类似的问题吗?你是怎么解决的?

EN

回答 1

Stack Overflow用户

发布于 2017-12-21 10:41:38

在css (sass,更少,不管)中,您不能动态地设置值。您应该预先为所有条件和所有方向生成所有属性。

因此,@media块应该放在元素选择器作用域中。在这些@media里面,你应该设定尺寸。

Sassmeister 演示.

Sass:

代码语言:javascript
复制
// $target: number in pixels
// $orientation: string - portrait (default) | landscape

@function get-vw($target, $orientation: portrait) { 
  $vw-context: (320 * .01) * 1px;
  $axis-unit: 1vw;

  @if ($orientation == landscape) {
    // Not shure about 240
    $vw-context: (240 * .01) * 1px;
    $axis-unit: 1vh;
  }

  @return ($target / $vw-context) * $axis-unit;
}

a {
  @media only screen and (orientation: landscape) {
    size: get-vw(12px, landscape);
  }
  @media only screen and (orientation: portrait) {
    size: get-vw(12px);
  }
}

Css:

代码语言:javascript
复制
@media only screen and (orientation: landscape) {
  a {
    size: 5vh;
  }
}

@media only screen and (orientation: portrait) {
  a {
    size: 3.75vw;
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47905198

复制
相关文章

相似问题

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