鼠标简单跟随提示二

Js代码 复制代码 收藏代码
  1. function id(s){
  2. return document.getElementById(s);
  3. }
  4. /**
  5. * 鼠标跟随提示
  6. */
  7. function mouseFollow() {
  8. var e = e ? e : window.event;
  9. var posx = e.clientX;
  10. var posy = e.clientY;
  11. var followDiv = id("followDiv") ? id("followDiv"): document.createElement("div");
  12. followDiv.setAttribute("id","followDiv");
  13. var css = "100px;height:20px;border:1px solid grey;position:absolute;z-index:10000;left:"+posx+";top:"+posy+";";
  14. setCss(followDiv,css);
  15. document.body.appendChild(followDiv);
  16. }
  17. /**
  18. * element:需要获取样式的目标元素;name:样式属性
  19. */
  20. function getStyle(element, name) {
  21. var computedStyle;
  22. try {
  23. computedStyle = document.defaultView.getComputedStyle(element, null);
  24. } catch (e) {
  25. computedStyle = element.currentStyle;
  26. }
  27. if (name != "float") {
  28. return computedStyle[name];
  29. } else {
  30. return computedStyle["cssFloat"] || computedStyle["styleFloat"];
  31. }
  32. }
  33. /**
  34. * element:需要设置样式的目标元素;name:样式属性;value:设置值
  35. */
  36. function setStyle(element, name, value) {
  37. if (name != "float") {
  38. element.style[name] = value;
  39. } else {
  40. element.style["cssFloat"] = value;
  41. element.style["styleFloat"] = value;
  42. }
  43. }
  44. /**
  45. *
  46. */
  47. function setCss(obj,css){
  48. obj.setAttribute("style",css);
  49. obj.style.cssText = css;
  50. }  
原文地址:https://www.cnblogs.com/bjanzhuo/p/3576101.html