使用 JavaScript 更改基于 iPad 方向的 SVG 图像代码
我需要一种方法来更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意
setAttribute()
不返回接收者。事实上,它没有返回值,根据规格。这不是 jQuery。让我们稍微干燥一下你的函数并同时修复它:
编辑:证明当代码运行时它会影响图像(至少在 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:
Edit: Proof that when the code is run it affects the image (in Chrome at least):
(source: phrogz.net)