有一个svg的图源,需要加载到页面上显示,对于不同屏幕大小的浏览器需要自适应全屏显示。有谁做过这方面?求教各位!
http://blog.csdn.net/kungstriving4/article/details/25186553
SVG自适应高度和宽度
<?xml version="1.0" encoding="UTF-8"?>
<?AdobeSVGViewer resolution="1200" save="snapshot"?>
<br> try<br> {<br> var oParentWin= (window.parent)?(window.parent):(window.__parent__);<br> var oParentBody= oParentWin.document.body;<br> var oSVG= null;<br> var fZoom= 1.0;<br> function onResize(event)<br> {<br> try<br> {<br> if (!oSVG)<br> {<br> window.focus();// <br> make sure oSVG= oParentWin.document.activeElement;<br><br> oParentBody.style.overflow= "auto";<br> setTimeout("oSVG=oParentWin.document.activeElement;onResize(null);", 250);<br> }<br> else<br> {<br> //((oSVG.tagName=="EMBED"))<br> try<br> {<br> if (!event)<br> return false;<br> switch((event.type)?(event.type):(""))<br> {<br> case "mousewheel":<br> {<br> var actDefaultAntialias= window.getDefaultAntialias();<br><br> if(actDefaultAntialias)<br> setDefaultAntialias(false);<br> if(isNaN(fZoom))<br> fZoom= 1.0;<br> var fInc= (event.wheelDelta >= 120)?(.1):(-.1);<br> fZoom= (fZoom>1)?(Math.floor(fZoom+fInc*10)):(fZoom+fInc);<br> fZoom= Math.max(.1, Math.min(10., fZoom));<br> oSVG.style.zoom= fZoom;<br> oSVG.height= oParentBody.clientHeight;<br> oSVG.width= oParentBody.clientWidth;<br> if(actDefaultAntialias)<br> setTimeout("window.setDefaultAntialias("+actDefaultAntialias+")", 1000);<br> oParentWin.status= "Zoom: " + oSVG.style.zoom;<br> }<br> break;<br> default:<br> {<br> oSVG.style.posHeight = oParentBody.clientHeight;<br> oSVG.style.posWidth= oParentBody.clientWidth;<br> oParentWin.status= ("Resized: [" + oParentBody.clientHeight + "x" + oParentBody.clientWidth + "]");<br> }<br> }//switch(evt.type)<br> }<br> catch(e)<br> { <br> alert(e.description);<br> }<br> }<br> }<br> catch(e)<br> {<br> alert(e.description);<br> }<br> }<br> {<br> oParentWin.attachEvent("onresize",onResize);<br> oParentWin.attachEvent("onbeforeprint", onResize);<br> oParentWin.document.attachEvent("onmousewheel", onResize);<br> };<br> }<br> catch(e)<br> {<br> alert(e.description);<br> }<br>
<br> <![CDATA[<br> @media print <br> {<br> }<br> ]]><br>
SVG自适应高度和宽度
<?xml version="1.0" encoding="UTF-8"?>
<?AdobeSVGViewer resolution="1200" save="snapshot"?>
<svg id="tstSVG" viewBox= "0 0 1000 1000" preserveAspectRatio= "xMidYMid meet" xmlns:xlink="http://www.w3.org/1999/xlink";>
<script>
try
{
var oParentWin= (window.parent)?(window.parent):(window.__parent__);
var oParentBody= oParentWin.document.body;
var oSVG= null;
var fZoom= 1.0;
function onResize(event)
{
try
{
if (!oSVG)
{
window.focus();//
make sure oSVG= oParentWin.document.activeElement;
oParentBody.style.overflow= "auto";
setTimeout("oSVG=oParentWin.document.activeElement;onResize(null);", 250);
}
else
{
//((oSVG.tagName=="EMBED"))
try
{
if (!event)
return false;
switch((event.type)?(event.type):(""))
{
case "mousewheel":
{
var actDefaultAntialias= window.getDefaultAntialias();
if(actDefaultAntialias)
setDefaultAntialias(false);
if(isNaN(fZoom))
fZoom= 1.0;
var fInc= (event.wheelDelta >= 120)?(.1):(-.1);
fZoom= (fZoom>1)?(Math.floor(fZoom+fInc*10)):(fZoom+fInc);
fZoom= Math.max(.1, Math.min(10., fZoom));
oSVG.style.zoom= fZoom;
oSVG.height= oParentBody.clientHeight;
oSVG.width= oParentBody.clientWidth;
if(actDefaultAntialias)
setTimeout("window.setDefaultAntialias("+actDefaultAntialias+")", 1000);
oParentWin.status= "Zoom: " + oSVG.style.zoom;
}
break;
default:
{
oSVG.style.posHeight = oParentBody.clientHeight;
oSVG.style.posWidth= oParentBody.clientWidth;
oParentWin.status= ("Resized: [" + oParentBody.clientHeight + "x" + oParentBody.clientWidth + "]");
}
}//switch(evt.type)
}
catch(e)
{
alert(e.description);
}
}
}
catch(e)
{
alert(e.description);
}
}
{
oParentWin.attachEvent("onresize",onResize);
oParentWin.attachEvent("onbeforeprint", onResize);
oParentWin.document.attachEvent("onmousewheel", onResize);
};
}
catch(e)
{
alert(e.description);
}
</script>
<style type="text/css">
<![CDATA[
@media print
{
}
]]>
</style>
<g style="fill:red; stroke:navy;">
<circle cx="500" cy="500" r="100" style="fill:gold;"/>
<path d="M0,495 l1000,0 l0,10 l-1000,0 z" />
<path d="M495,0 l10,0 l0,1000 l-10,0 z" />
</g>
</svg>
同问,如过svg里面还有text、tspan等节点,怎么做到连着text和tspan一同缩放,我之前用过scale,不怎么灵活。
CSS中用vw作为尺寸标识。100VW自动等于100%窗口宽度,高度可不设置会自适应。
type="image/svg+xml"
pluginspage="http://www.adobe.com/svg/viewer/install/" />
text和tspan也可以用vw来控制宽度随屏幕变化,此标签适合用在手机浏览器上使用
<embed src="test.svg" width="100vw"
type="image/svg+xml"
pluginspage="http://www.adobe.com/svg/viewer/install/" />
vw:viewpoint width,视窗宽度,1vw等于视窗宽度的1%。
vh:viewpoint height,视窗高度,1vh等于视窗高度的1%。
vmin:vw和vh中较小的那个。
vmax:vw和vh中较大的那个。
vw, vh, vmin, vmax:IE9+局部支持,chrome/firefox/safari/opera支持,iOS safari 8+支持,Android browser4.4+支持,chrome for android39支持