预览模式: 普通 | 列表

使用JQuery使Div居中

Div居中是一个比较常见的需求。下面介绍一种使用JQuery使Div居中的方法
先假设有这样一个Div,<div id="d">test</div>
首先是要把需要居中的Div进行绝对定位,如:
<style type="text/css">
#d {
position: absolute;
width: 500;
height: 300;
border: 1px solid red;
}
</style>
有了这样的样式定义之后就是使其居中的js代码了。

JavaScript代码
  1. var obj = $("#d");//获得相应的Div对象    
  2. var x = ($(window).width()-500)/2;//使用$(window).width()获得显示器的宽,并算出对应的Div离左边的距离    
  3. var y = ($(window).height()-300)/2;//使用$(window).height()获得显示器的高,并算出相应的Div离上边的距离    
  4. obj.css("top",y).css("left",x);  

上面这段代码可以在页面一加载完就执行。

改进后的代码:

JavaScript代码
  1. $(document).ready(function(){  
  2.     middle();  
  3. });  
  4. $(window).resize(function(){  
  5.     middle();  
  6. });  
  7. function middle(){  
  8.     var obj = $("#mainbg");//获得相应的Div对象    
  9.     var x = ($(window).width()-500)/2;//使用$(window).width()获得显示器的宽,并算出对应的Div离左边的距离    
  10.     var y = ($(window).height()-300)/2;//使用$(window).height()获得显示器的高,并算出相应的Div离上边的距离    
  11.     obj.css("top",y).css("left",x);   
  12. }  

上面的代码实现窗口大小变化后始终居中。

Tags: javascript jquery

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2080

css背景颜色渐变

 垂直方向:

CSS代码
  1. filter: progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FF0000',endColorStr='#F9F900',gradientType='0');  
  2.   
  3. background: -moz-linear-gradient(top#FF0000#F9F900);  
  4.   
  5. background: -o-linear-gradient(top,#FF0000#F9F900);  
  6.   
  7. background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#FF0000), to(#F9F900));  

水平方向:

CSS代码
  1. filter: progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FF0000',endColorStr='#F9F900',gradientType='1');  
  2.   
  3. background: -moz-linear-gradient(left#FF0000#F9F900);  
  4.   
  5. background: -o-linear-gradient(left,#FF0000#F9F900);  
  6.   
  7. background: -webkit-gradient(linear, 0% 0%, 100% 0%, from(#FF0000), to(#F9F900));  

Tags: css

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2124

jQuery禁止鼠标右键

 jQuery屏蔽鼠标右键:
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
--------------------------------------------------------------------------------------------------
JS屏蔽鼠标右键:
<script language="JavaScript">
document.oncontextmenu=new Function("event.returnValue=false;");
document.onselectstart=new Function("event.returnValue=false;");
</script>
--------------------------------------------------------------------------------------------------
jquery禁止右键弹出的突破方法:
如: $(document).bind("contextmenu", function() { return false; }); //这段JS代码将禁止右键弹出
突破方法:
地址栏中输入:javascript:alert($(document).unbind("contextmenu",""));

Tags: javascript jquery

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2669

JavaScript获取当前根目录

主要用到Location 对象,包含有关当前 URL 的信息,是 Window 对象的一个部分,可通过 window.location 属性来访问。

方法一 (window.document.location.href/window.document.location.pathname) 

JavaScript代码
  1. function getRootPath_web() {  
  2.             //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp  
  3.             var curWwwPath = window.document.location.href;  
  4.             //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp  
  5.             var pathName = window.document.location.pathname;  
  6.             var pos = curWwwPath.indexOf(pathName);  
  7.             //获取主机地址,如: http://localhost:8083  
  8.             var localhostPaht = curWwwPath.substring(0, pos);  
  9.             //获取带"/"的项目名,如:/uimcardprj  
  10.             var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);  
  11.             return (localhostPaht + projectName);  
  12.         }  

方法二(window.location.pathname/window.location.protocol/window.location.host)

JavaScript代码
  1. function getRootPath_dc() {  
  2.             var pathName = window.location.pathname.substring(1);  
  3.             var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));  
  4.             if (webName == "") {  
  5.                 return window.location.protocol + '//' + window.location.host;  
  6.             }  
  7.             else {  
  8.                 return window.location.protocol + '//' + window.location.host + '/' + webName;  
  9.             }  
  10.         }  

注:

查看更多...

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 1675

 广告位

↑返回顶部↑