1. SVG背景透明的解决办法
IE中,完全可以支持SVG透明。
条件:使用在SVG中会经常遇到判断鼠标事件来源的问题,比如:鼠标单击或者双击、滚轮事件等等。这里做一个简单的介绍。
判断鼠标是左键还是右键?
在onclick事件中,if(evt.button==0)则为左击,否则为右击
无论单击还是双击evt.detail==1
判断鼠标是单击还是双击?
在onclick事件中,if(evt.detail==2)则为双击,否则为单击
判断鼠标的滚轮事件?
function
mousewheel()
{
origscale=root.currentScale;
origscale +=event.wheelDelta / 1200;
if (origscale > 0)
{
root.currentScale=origscale;
root.currentTranslate.x=midx*root.currentScale+event.offsetX*(1-root.currentScale/midscale);
root.currentTranslate.y=midy*root.currentScale+event.offsetY*(1-root.currentScale/midscale);
midscale=root.currentScale;
midx=root.currentTranslate.x/root.currentScale;
midy=root.currentTranslate.y/root.currentScale;
}
}
4. SVG自适应高度和宽度

origscale=root.currentScale;
origscale +=event.wheelDelta / 1200;
if (origscale > 0)

root.currentScale=origscale;
root.currentTranslate.x=midx*root.currentScale+event.offsetX*(1-root.currentScale/midscale);
root.currentTranslate.y=midy*root.currentScale+event.offsetY*(1-root.currentScale/midscale);
midscale=root.currentScale;
midx=root.currentTranslate.x/root.currentScale;
midy=root.currentTranslate.y/root.currentScale;
}
}
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" >
@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 >
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" >
@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 >