个人笔记015--js路由和css实现元素垂直居中

本文通过一个实例,展示了如何使用原生JS实现页面路由,并结合三种不同的CSS技巧实现元素的水平垂直居中,包括绝对定位、transform和Flex布局。

这两天时间比较清闲,想学习一下原生js实现路由的demo,查询了好多别人的博客和一些资料,顺便做一下元素垂直居中就做成了下面这个样子:

index.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js路由和css实现元素垂直居中</title>
    <style>
		.nav{
			text-align:center;
		}
        .nav li{
            display:inline-block;
            list-style:none;
        }
        .nav li a{
            display:inline-block;
            text-decoration: none;
            padding:10px 15px;
        }
        a{
            cursor: pointer;
        }
    </style>
    
</head>
<body>
    <section class="warp">
        <div class="nav">
            <ul>
                <li><a href="javascript:void(0)" data-path="one">第一种</a></li> 
                <li><a href="javascript:void(0)" data-path="two">第二种</a></li>
                <li><a href="javascript:void(0)" data-path="three">第三种</a></li>
            </ul>
        </div>
        <div id="router" class="router">
            <!-- 内容加载区域 -->
        </div>
    </section>
    
    <script src="jquery-3.4.0.min.js"></script>
    <script src="router.js"></script>
 
</body>
</html>

router.js代码:

(function(){  	  	
  	var router = [
  		{
			  'path':'one',
			  'url':'./centered_one.html'
		  },
		  {
			  'path':'two',
			  'url':'./centered_two.html'
		  },
		  {
			  'path':'three',
			  'url':'./centered_three.html'
		  }
  	];
  	//改变页面
  	function change_page(url){
  		$('#router').load(url)
  	}
	$('a').on('click',function(){
		var path = $(this).data('path');
		for(var i in router){
			$('li')[i].style.background = '#fff';	
			$('li a')[i].style.color = 'blue';
			if(router[i].path == path){
				$('li')[i].style.background = 'blue';
				$('li a')[i].style.color = '#fff';
				change_page(router[i].url);
				history.pushState(router[i].url,null,'#/'+router[i].path);
			}
		}
	})
	window.addEventListener('popstate',function(e){
		var url = e.state;
	    change_page(url);
	    
	});
	window.addEventListener('load',function(){
		var path = location.hash.slice(2) || '/one';
		for(var i in router){//刷新 加载
			if(router[i].path == path){
				$('li')[i].style.background = 'blue';
				$('li a')[i].style.color = '#fff';
				change_page(router[i].url);	
				history.replaceState(router[i].url,null,'#/' + path);		
				break;	
			}
			if(i == router.length-1){//重定向
				change_page(router[0].url);	
				history.replaceState(router[0].url,null,'#/' + router[0].path);	
			}
		}						
	});

})();	

centered_one.html代码(即第一种css实现元素水平垂直居中):

 <style>
        #father{
            width: 500px;
            height: 500px;
            background-color: skyblue;
            position: relative;
            margin: 0 auto;
        }
        .centered{
            width: 200px;
            height: 200px;
            padding: 10px;
            background-color: green;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
        }
        .centered p{
            color: #fff;;
        }
    </style>
<section>
    <div id="father">
        <div class="centered">
            <p>111111111js路由和css实现元素垂直居中111111111</p>
        </div>
    </div>
</section>

centered_two.html代码(即第二种css实现元素水平垂直居中):

   <style>
        #father{
            width: 500px;
            height: 500px;
            background-color: skyblue;
            position: relative;
            margin: 0 auto;
        }
        .centered{
            min-width: 180px;
            min-height: 180px;
            padding: 10px;
            background-color: green;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translateX(-50%) translateY(-50%);
        }
        .centered p{
            color: #fff;
        }
    </style>
<section>
    <div id="father">
        <div class="centered">
            <p>222222js路由和css实现元素垂直居中2222222</p>
        </div>
    </div>
</section>

centered_three.html代码(即第三种css实现元素水平垂直居中):


<style>
    #father{
        width: 500px;
        height: 500px;
        background-color: skyblue;
        display: flex;
        justify-content: center;
        align-items: center;
        margin: 0 auto;
    }
    .centered{
        width: 200px;
        height: 200px;
        padding: 10px;
        background-color: green;
    }
    .centered p{
        color: #fff;
    }
</style>
<section>
    <div id="father">
        <div class="centered">
            <p>3333333333js路由和css实现元素垂直居中33333333</p>
        </div>
    </div>
</section>

PS:以上代码如果直接在桌面运行会报错:Cross origin requests are only supported for protocol schemes: http, data, chrome, se-extension, https,当然火狐浏览器是没问题的

以下是几个解决方法:

1.换编辑器

使用webstorm或是visual studio这种重量级的IDE(此类IDE都内置了HTTP服务器)。 

2.安装Server插件: 
这里针对VSCode和Sublime介绍一款插件—VSCode,这个插件功能十分强大,不止可以解决ajax不支持file协议的问题,还可以实现在编辑器里保存修改后,页面自动刷新的功能

3.浏览器

1)直接用火狐打开即可;

2)Chrome:

Windows: 
设置Chrome的快捷方式属性,在“目标”后面加上–allow-file-access-from-files,注意前面有个空格。

Mac: 
打开终端,输入下面命令:open -a “Google Chrome” –args –disable-web-security然后就可以屏蔽安全访问了

 

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值