【问题标题】:Change SVG image code based on iPad orientation with JavaScript使用 JavaScript 根据 iPad 方向更改 SVG 图像代码
【发布时间】:2019-08-14 01:06:57
【问题描述】:

我需要一种方法来更改 SVG 内部的图像宽度和高度,基于设备的方向,使用 Javascript(或 SVG 内部的 ECMAScript)。

我正在使用此代码,但它不会影响 SVG 中的图像元素。

// SVG code, inside XHTML file
<svg xmlns="http://www.w3.org/2000/svg" class="birds" height="569px" id="bird01" version="1.1" width="368px" x="0px" xml:space="preserve" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">
  <g>
    <defs>
      <rect class="crop" height="569" id="SVGID_1_" width="368"></rect>
    </defs>
    <use overflow="visible" xlink:href="#SVGID_1_"></use>
    <g clip-path="url(#SVGID_2_)">
      <image clip-path="url(#SVGID_2_)" height="846" id="bird-img" overflow="visible" width="547" xlink:href="../Images/bird.jpeg"></image>
    </g>
  </g>
</svg>

// JS code, outside file, referenced in head

window.onload = function initialLoad() {
     detectOrientation();
}

detectOrientation();
    window.onorientationchange = detectOrientation;
    function detectOrientation(){
        if(typeof window.onorientationchange != 'undefined'){
            if ( orientation == 0 ) {
                 document.getElementById("bird-img").setAttribute("width", "547").setAttribute("height", "846");
            }
            else if ( orientation == 90 ) {
                 document.getElementById("bird-img").setAttribute("width", "368").setAttribute("height", "568");
            }
            else if ( orientation == -90 ) {
                 document.getElementById("bird-img").setAttribute("width", "368").setAttribute("height", "568");
            }
            else if ( orientation == 180 ) {
                 document.getElementById("bird-img").setAttribute("width", "547").setAttribute("height", "846");
            }
        }
    }

此链接显示一个测试页面:http://bit.ly/mSBDhq 调整窗口大小以查看 SVG 框的大小变化。我只想在两种情况下(横向和纵向)将图像大小调整为 SVG 框的宽度。我使用 CSS 媒体查询来完成大部分工作,但 SVG 图像元素不受 CSS 影响,这就是我正在寻找 JS 解决方案的原因。我很欣赏这方面的任何亮点。

旁注:不幸的是,在这种情况下,我根本无法使用常规的图像标签和 CSS 属性溢出结合媒体查询来实现这种效果(长话短说)。

【问题讨论】:

  • 在调用函数之前声明函数有帮助吗?
  • 你能澄清发生了什么吗? detectOrientation 被叫到了吗?您是否进入了正确的 if 语句分支? (顺便说一句,为了简洁起见,您可能想在这里使用switch。)getElementById 返回正确的对象吗?设置属性后,它们是否发生了变化(在 DOM 中),但您在屏幕上看不到任何效果?
  • Phrogz 的回答有助于解决一些问题,但即使在此之后元素不受影响......也许 js 无法到达 svg 内部。

标签: javascript ipad svg orientation


【解决方案1】:

请注意,setAttribute() 不会返回接收者。事实上,它has no return value,根据规范。这不是 jQuery。

让我们稍微干一下你的函数并同时修复它:

function detectOrientation(){
  if(typeof window.onorientationchange == 'undefined') return;
  var img = document.getElementById('bird-img');
  switch(orientation){
    case 0:
    case 180:
      img.setAttribute('width',547);
      img.setAttribute('height',846);
    break;
    case 90:
    case -90:
      img.setAttribute('width',368);
      img.setAttribute('height',568);
    break;
  }
}

编辑:证明代码在运行时会影响图像(至少在 Chrome 中):


(来源:phrogz.net

【讨论】:

  • 谢谢,Phrogz,您的建议解决了原始 JS 的问题。但是 de SVG 中的“图像”元素尚未受到影响,请看:bit.ly/mSBDhq。如果我在 SVG 代码之外使用常规的 img 标签,一切都会很好。但是图像元素没有运气。可能是因为svg有自己的命名空间,但是不知道怎么把js代码和它集成起来……
  • @pagelab 我没有 iPad 来测试这个,但是在 Chrome 中你的 JS 函数没有被调用。请参阅对我的问题的编辑,以证明如果调用 JS,它应该可以工作。
  • 是的。我尝试了几种调用方法都没有运气。看不出为什么该功能不起作用。
  • @pagelab 也许您需要创建一个仅包含该主题的新问题:如何检测 JS 中的方向变化。
猜你喜欢
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
相关资源
最近更新 更多