我寫了這個功能:的JavaScript的createElement(),風格問題今天
function zoom(obj){
var img = (!document.getElementById(obj))?false:document.getElementById(obj);
var fullImage = (img.getAttribute("image") == null)?false:img.getAttribute("image");
if(!fullImage || !img) { return false; }
if(!document.getElementById("::img")) {
var ob = "<div id='::img' style='position:absolute;top:300px;left:300px;width:200px;height:200px;-moz-border-radius:100px;border:1px solid #ddd;-moz-box-shadow: 0px 0px 8px #fff;display:none;'></div>";
document.write(ob);}
img.onmousemove = function(e) {
var x = Math.floor(((e.pageX-7) - (img.offsetLeft - 8)) * 100/img.width);
var y = Math.floor(((e.pageY-7) - (img.offsetTop - 8)) * 100/img.height);
x = (x>100)?100:(x<0)?0:x;
y = (y>100)?100:(y<0)?0:y;
var ob = document.getElementById("::img");
ob.style.background = "url('" + fullImage + "') no-repeat";
ob.style.display = 'block';
ob.style.backgroundPosition = x + '% ' + y + '%';
ob.style.left = e.pageX + "px";
ob.style.top = e.pageY + "px";
}
img.onmouseout = function() { document.getElementById("::img").style.display='none'; }
}
我試圖用的createElement(),並使用appendChild()函數,而不是這樣的:
var ob = `""`; document.write(ob);
但函數不起作用,如何使它與createElement()一起工作。
是否有可能與一行代碼添加的所有風格,純JAVASCRIPT:
ob.style.background = "url('" + fullImage + "') no-repeat";
ob.style.display = 'block';
ob.style.backgroundPosition = x + '% ' + y + '%';
ob.style.left = e.pageX + "px";
ob.style.top = e.pageY + "px";
前瞻:JsFiddle
omg有趣,但我30分鐘前曾嘗試過同樣的事情,它沒有工作(我想我錯過了什麼)..多數民衆贊成爲什麼我用document.write ...謝謝 – John 2011-05-08 11:17:47
謝謝,它爲我工作。 – 2014-04-17 10:00:33