使用 JavaScript 更改基于 iPad 方向的 SVG 图像代码

发布于 2024-12-10 13:44:24 字数 2073 浏览 1 评论 0原文

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

我正在使用这段代码,但它不会影响 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 属性溢出与媒体查询相结合来实现这种效果(长话短说)。

I need a way to change an image width and height, inside an SVG, based on the orientation of the device, using Javascript (or ECMAScript, inside the SVG).

I'm using this code, but it's not affecting the image element inside the 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");
            }
        }
    }

This link show a test page: http://bit.ly/mSBDhq Adjust the window size to see the SVG box changing it's size. I just want the image size adjusted to the width of SVG box in both cases (landscape and portrait). I'm using CSS media queries to do most of the work, but the SVG image element isn't affected by the CSS, that's the reason I'm looking for an JS solution. I appreciate any lights on this.

Side note: unfortunately, in this case, I simply can not use a regular image tag and CSS property overflow combined with media queries to achieve this effect (long story).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2024-12-17 13:44:24

请注意 setAttribute() 不返回接收者。事实上,它没有返回值,根据规格。这不是 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

Note that setAttribute() does not return the receiver. Indeed, it has no return value, per the spec. This is not jQuery.

Let's DRY up your function a little and fix it at the same time:

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;
  }
}

Edit: Proof that when the code is run it affects the image (in Chrome at least):

enter image description here
(source: phrogz.net)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文