Created
March 30, 2014 18:59
-
-
Save rockstar2046/9877819 to your computer and use it in GitHub Desktop.
Show a qrcode image on browser lower right | left corner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @fileOverview Show a qrcode image on browser lower right | left corner | |
* @author ra <ra@rockagen.com> | |
* @version 0.0.1 | |
* @see {@link http://usejsdoc.org|jsdoc} | |
* | |
* | |
* @example | |
* | |
* qrsource:{ | |
* img: img url | |
* href: link url | |
* } | |
* | |
* pos: default lower right corner,if pos is undefined. | |
* | |
* ------------------------------------------------------------------------- | |
* | |
* sqrcode(qrsource, pos) | |
* | |
* | |
* sqrcode({ | |
* img:"http://img3.cache.netease.com/news/2012/qr_newapp.png", | |
* href:"http://m.163.com/newsapp/" | |
* }); | |
* | |
* or | |
* | |
* sqrcode({ | |
* img:"http://img3.cache.netease.com/news/2012/qr_newapp.png", | |
* href:"http://m.163.com/newsapp/" | |
* },1); | |
*/ | |
/** @namespace | |
*/ | |
var sqrcode=function(qrsource,pos){ | |
if (typeof qrsource == "undefined") | |
qrsource = {}; | |
if (typeof qrsource.img == "undefined") | |
qrsource.img="xxx.png"; | |
if (typeof qrsource.href == "undefined") | |
qrsource.href="#"; | |
var version = window.navigator.appVersion; | |
var isIE6 = (version.indexOf("MSIE 6.0") != -1 || version.indexOf("MSIE 5.5") != -1)? true:false; | |
function showQR(top){ | |
var QRbox= document.createElement("div"); | |
var QRboxCSS ={ | |
'width': "110px", | |
'height': "147px", | |
'position': "fixed", | |
'_position': "absolute", | |
'top': top + "px", | |
'display':"block", | |
'zIndex':100 | |
}; | |
for(var key in QRboxCSS){ | |
QRbox.style[key] = QRboxCSS[key]; | |
} | |
if(!!pos){ | |
QRbox.style.left="3px"; | |
}else{ | |
QRbox.style.right="3px"; | |
} | |
if(isIE6){ | |
QRbox.style.position = "absolute"; | |
} | |
QRbox.innerHTML="<a href='"+qrsource.href+"' target='_blank'></a>"; | |
var link =QRbox.getElementsByTagName("a")[0]; | |
var linkCSS = { | |
display:"block", | |
width: "100%", | |
height: "100%", | |
backgroundImage: "url("+qrsource.img+")", | |
backgroundPosition: "0px 0px" | |
}; | |
for(var key1 in linkCSS){ | |
link.style[key1] = linkCSS[key1]; | |
} | |
document.body.appendChild(QRbox); | |
var linkEventMap = { | |
"mouseover": function(){ | |
link.style.backgroundPosition = "-110px 0px"; | |
}, | |
"mouseout": function(){ | |
link.style.backgroundPosition = "0px 0px"; | |
} | |
}; | |
for(var evt in linkEventMap){ | |
var func = linkEventMap[evt]; | |
addEvent(evt, link, func); | |
} | |
addEvent("scroll", window, function(){ | |
if(isIE6){ | |
QRbox.style.top = getScrollTop() + top + "px"; | |
} | |
}); | |
} | |
function addEvent(evt, elem, callback){ | |
if(elem.addEventListener){ | |
elem.addEventListener(evt, callback, false); | |
} else { | |
elem.attachEvent && elem.attachEvent("on"+evt, callback); | |
} | |
} | |
function getScrollTop(){ | |
if(typeof pageYOffset!= 'undefined'){ | |
//most browsers | |
return pageYOffset; | |
} | |
else{ | |
var B= document.body; //IE 'quirks' | |
var D= document.documentElement; //IE with doctype | |
D= (D.clientHeight)? D: B; | |
return D.scrollTop; | |
} | |
} | |
return showQR(500); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment