[关闭]
@EncyKe 2015-12-28T07:21:28.000000Z 字数 6838 阅读 1482

JS:可拖曳登录面板

前端 JavaScript



HTML部分

  1. <div class="loginPanel" id="loginPanel">
  2. <div style="position: relative; z-index: 1;">
  3. <div class="ui_boxyClose" id="ui_boxyClose"></div>
  4. </div>
  5. <div class="login_logo_webqq"></div>
  6. <div class="inputs">
  7. <div class="sign-input"><span>帐 号:</span><span><input autocomplete="on" name="u" id="u" type="text" style="ime-mode: disabled" class="input01" tabindex="1" value="QQ号码或Email帐号" onFocus="if (value =='QQ号码或Email帐号'){value =''}" onBlur="if (value ==''){value='QQ号码或Email帐号';}" /></span></div>
  8. <div class="sign-input"><span>密 码:</span><span><input name="p" id="p" maxlength="16" type="password" class="input01" tabindex="2" /></span></div>
  9. </div>
  10. <div class="bottomDiv">
  11. <div class="btn" style="float: left"></div>
  12. <div>
  13. <div id="loginState" class="login-state-trigger login-state-trigger2 login-state" title="选择在线状态">
  14. <div id="loginStateShow" class="login-state-show online">状态</div>
  15. <div class="login-state-down"></div>
  16. <div class="login-state-txt" id="login2qq_state_txt">在线</div>
  17. <ul id="loginStatePanel" class="statePanel login-state" style="display: none">
  18. <li id="online" class="statePanel_li">
  19. <div class="stateSelect_icon online"></div>
  20. <div class="stateSelect_text">我在线上</div>
  21. </li>
  22. <li id="callme" class="statePanel_li">
  23. <div class="stateSelect_icon callme"></div>
  24. <div class="stateSelect_text">Q我吧</div>
  25. </li>
  26. <li id="away" class="statePanel_li">
  27. <div class="stateSelect_icon away"></div>
  28. <div class="stateSelect_text">离开</div>
  29. </li>
  30. <li id="busy" class="statePanel_li">
  31. <div class="stateSelect_icon busy"></div>
  32. <div class="stateSelect_text">忙碌</div>
  33. </li>
  34. <li id="silent" class="statePanel_li">
  35. <div class="stateSelect_icon silent"></div>
  36. <div class="stateSelect_text">请勿打扰</div>
  37. </li>
  38. <li id="hidden" class="statePanel_li">
  39. <div class="stateSelect_icon hidden"></div>
  40. <div class="stateSelect_text">隐身</div>
  41. </li>
  42. </ul>
  43. </div>
  44. </div>
  45. </div>
  46. </div>

CSS部分

  1. .loginPanel {
  2. width: 380px;
  3. height: 247px;
  4. left: 400px;
  5. top: 120px;
  6. position: absolute;
  7. border: 1px solid #ccc;
  8. background: #f6f6f6;
  9. -moz-border-radius: 10px;
  10. -webkit-border-radius: 10px;
  11. border-radius: 10px;
  12. -moz-box-shadow: 0 0 8px #000;
  13. -webkit-box-shadow: 0 0 8px #000;
  14. box-shadow: 0 0 8px #000;
  15. }
  16. .login_logo_webqq {
  17. background: url('../images/login_window_logo.png') no-repeat -210px -0px;
  18. margin-left: 100px;
  19. margin-top: 10px;
  20. width: 200px;
  21. height: 44px;
  22. cursor: move;
  23. }
  24. .inputs {
  25. font: bold 15px arial;
  26. margin-left: 80px;
  27. margin-top: 30px;
  28. }
  29. .inputs .sign-input {
  30. padding-bottom: 20px;
  31. }
  32. .inputs .sign-input input {
  33. width: 170px;
  34. border: 1px #ccc solid;
  35. color: #868686;
  36. font-size: 16px;
  37. padding: 2px;
  38. -moz-border-radius: 10px;
  39. -webkit-border-radius: 10px;
  40. -khtml-border-radius: 10px;
  41. -border-radius: 10px;
  42. outline: none;
  43. }
  44. .btn {
  45. background: url("../images/login_btn.png") no-repeat -111px 0;
  46. width: 111px;
  47. height: 36px;
  48. border: 0;
  49. text-align: center;
  50. line-height: 20px;
  51. color: #0C4E7C;
  52. cursor: pointer;
  53. margin-left: 14px;
  54. }
  55. .login-state-trigger {
  56. cursor: pointer;
  57. display: block;
  58. float: left;
  59. height: 16px;
  60. overflow: hidden;
  61. width: 120px;
  62. margin: 4px 0 0 0;
  63. }
  64. .login-state-trigger2 {
  65. margin: 10px 0 0 20px;
  66. }
  67. .login-state-down {
  68. background: url("../images/ptlogin.png") no-repeat scroll 0 -22px transparent;
  69. float: left;
  70. height: 6px;
  71. margin-top: 5px;
  72. overflow: hidden;
  73. text-indent: -999em;
  74. width: 7px;
  75. }
  76. .login-state-show {
  77. float: left;
  78. height: 14px;
  79. overflow: hidden;
  80. text-indent: -999em;
  81. width: 14px;
  82. margin: 1px 4px 0 0;
  83. }
  84. .login-state-txt {
  85. float: left;
  86. margin-left: 5px;
  87. font-size: 12px;
  88. >line-height:18px!important;
  89. }
  90. .login-state .callme {
  91. background: url("../images/ptlogin.png") -72px 0 no-repeat;
  92. }
  93. .login-state .online {
  94. background: url("../images/ptlogin.png") 0 0 no-repeat;
  95. }
  96. .login-state .away {
  97. background: url("../images/ptlogin.png") -18px 0 no-repeat;
  98. }
  99. .login-state .busy {
  100. background: url("../images/ptlogin.png") -36px 0 no-repeat;
  101. }
  102. .login-state .silent {
  103. background: url("../images/ptlogin.png") -108px 0 no-repeat;
  104. }
  105. .login-state .hidden {
  106. background: url("../images/ptlogin.png") -54px 0 no-repeat;
  107. }
  108. .statePanel {
  109. display: none;
  110. position: absolute;
  111. right: 68px;
  112. top: 193px;
  113. z-index: 10;
  114. margin: 0;
  115. border-width: 1px;
  116. border-style: solid;
  117. border-color: #ccc #6a6a6a #666 #cdcdcd;
  118. padding: 0;
  119. width: 100px;
  120. height: 133px;
  121. overflow: hidden;
  122. background: white;
  123. font-size: 12px;
  124. line-height: 1.5;
  125. }
  126. .statePanel .statePanel_li {
  127. display: block;
  128. float: left;
  129. margin: 0;
  130. padding: 3px 0;
  131. width: 100px;
  132. height: 16px;
  133. line-height: 16px;
  134. overflow: hidden;
  135. zoom: 1;
  136. cursor: pointer;
  137. }
  138. .stateSelect_icon {
  139. float: left;
  140. margin: 2px 0 0 5px;
  141. width: 14px;
  142. height: 14px;
  143. overflow: hidden;
  144. }
  145. .stateSelect_text {
  146. margin: 0 0 0 22px;
  147. }
  148. .bottomDiv {
  149. margin-left: 70px;
  150. }
  151. .ui_boxyClose{
  152. width:28px;
  153. height:28px;
  154. position:absolute;
  155. top:-10px;
  156. right:-10px;
  157. cursor:pointer;
  158. background:url('../images/boxy_btn.png') no-repeat;z-index:1
  159. }
  160. .ie6_0 .ui_boxyClose{
  161. background:0;
  162. filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='boxy_btn.png',sizingMethod='scale')
  163. }

JS部分

  1. window.onload = drag;
  2. // 封装类名选择函数
  3. function getByClass(clsName,parent){
  4. var oParent = parent ? document.getElementById(parent) : document,
  5. eles = [],
  6. elements = oParent.getElementsByTagName('*');
  7. for(var i = 0, l = elements.length; i < l; i++){
  8. if(elements[i].className == clsName){
  9. eles.push(elements[i]);
  10. }
  11. }
  12. return eles;
  13. }
  14. // 拖曳主函数
  15. function drag(){
  16. var oTitle = getByClass('login_logo_webqq','loginPanel')[0];
  17. // 拖曳
  18. oTitle.onmousedown = fnDown;
  19. // 关闭
  20. var oClose = document.getElementById('ui_boxyClose');
  21. oClose.onclick = function(){
  22. document.getElementById('loginPanel').style.display = 'none';
  23. }
  24. // 获取切换状态
  25. var loginState = document.getElementById('loginState'),
  26. stateList = document.getElementById('loginStatePanel'),
  27. lis = stateList.getElementsByTagName('li'),
  28. stateTxt = document.getElementById('login2qq_state_txt'),
  29. loginStateShow = document.getElementById('loginStateShow');
  30. // 状态按钮下拉菜单
  31. loginState.onclick = function(e){
  32. e = e || window.event;
  33. if(e.stopPropagation){
  34. e.stopPropagation();
  35. }
  36. else{
  37. e.cancelBubble = true;
  38. }
  39. stateList.style.display='block';
  40. }
  41. // 鼠标滑过、离开和点击状态列表时
  42. for(var i = 0, l = lis.length; i < l; i++){
  43. lis[i].onmouseover = function(){
  44. this.style.background = '#567';
  45. }
  46. lis[i].onmouseout = function(){
  47. this.style.background = '#FFF';
  48. }
  49. lis[i].onclick=function(e){
  50. e = e || window.event;
  51. if(e.stopPropagation){
  52. e.stopPropagation();
  53. }
  54. else{
  55. e.cancelBubble = true;
  56. }
  57. var id = this.id;
  58. stateList.style.display = 'none';
  59. stateTxt.innerHTML = getByClass('stateSelect_text',id)[0].innerHTML;
  60. loginStateShow.className = '';
  61. loginStateShow.className = 'login-state-show ' + id;
  62. }
  63. }
  64. document.onclick = function(){
  65. stateList.style.display = 'none';
  66. }
  67. }
  68. // 鼠标按住事件
  69. function fnDown(event){
  70. event = event || window.event;
  71. var oDrag = document.getElementById('loginPanel'),
  72. // 光标按下时光标和面板之间的距离
  73. disX = event.clientX - oDrag.offsetLeft,
  74. disY = event.clientY - oDrag.offsetTop;
  75. // 移动
  76. document.onmousemove = function(event){
  77. event = event || window.event;
  78. fnMove(event,disX,disY);
  79. }
  80. // 释放鼠标
  81. document.onmouseup = function(){
  82. document.onmousemove = null;
  83. document.onmouseup = null;
  84. }
  85. }
  86. // 鼠标拖曳事件
  87. function fnMove(e,posX,posY){
  88. var oDrag = document.getElementById('loginPanel'),
  89. l = e.clientX - posX,
  90. t = e.clientY - posY,
  91. winW = document.documentElement.clientWidth || document.body.clientWidth,
  92. winH = document.documentElement.clientHeight || document.body.clientHeight,
  93. maxW = winW - oDrag.offsetWidth - 10,
  94. maxH = winH - oDrag.offsetHeight;
  95. if(l < 0){
  96. l = 0;
  97. }
  98. else if(l > maxW){
  99. l = maxW;
  100. }
  101. if(t < 0){
  102. t = 10;
  103. }
  104. else if(t > maxH){
  105. t = maxH;
  106. }
  107. oDrag.style.left = l + 'px';
  108. oDrag.style.top = t + 'px';
  109. }

附:参考

慕课:DOM事件探秘-第四章
Demo下载

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注