前言
之前就想通过博客园提供的自定义样式功能美化一下博客,但是一直想等学会 vue 再来改(其实是懒......)。上个礼拜看到一位大神的博客感到非常惊艳,遂下定决心用一个周末来改。之前有想过自己设计样式,但是感觉越看越丑,所以还是决定照着这位大神的博客一点点改。
准备工作
1、先看看有没有轮子
Google 一下,没有喜欢的样式。
上 Github 也没有搜到想要的。
可能关键词没用对吧。
2、自己动手,丰衣足食
首先要申请js权限,要不然很多东西都实现不了。然后右键查看源代码,把html下载下来(方便在本地调试)。最后照着别人的博客一点点的修改,一步一步的实现效果。
ps:我原本用的博客样式是bluesky,其实还是蛮喜欢的
3、最后成果
修改前:
修改后:
美化样式
代码以 Github上的为最新版
页面定制CSS代码
/** 原有样式修改 */
#sideBar{
display: none;
}
#mainContent{
width: 100%;
box-shadow: 0 0 0;
border-radius: 0px;
opacity: 0;
transition:0.5s;
margin-top:40px;
}
#main{
width: 40%;
background-color: white;
/*max-width: 700px;*/
}
@media screen and (max-width: 1000px) {
#main {width: 100%;}
}
body{
background-image: none;
background-color: white;
font-family: Menlo,Bitstream Vera Sans Mono,DejaVu Sans Mono,Monaco,Consolas,monospace !important;
line-height: 1.8;
}
#cnblogs_post_body{
font-size: 16px;
}
#green_channel{
display: none;
}
#post_next_prev{
display: none;
}
.forFlow img{
margin-top: 0;
}
/** 去除广告 */
#cb_post_title_url{
display: none;
}
#cnblogs_c1{
display: none;
}
#cnblogs_c2{
display: none;
}
#ad_t2{
display: none;
}
#kb_block{
display:none
}
#under_post_news{
display:none
}
#header{
display:none
}
#BlogPostCategory{
display: none;
}
#comment_nav{
display: none;
}
.postDesc{
border-bottom:none;
}
#author_profile_follow{
display: none;
}
#comment_form_container{
display: none;
}
/** 自定义样式 */
/* 加载条 */
#myProgressBar{
width: 15%;
height: 2px;
background-color: #eb5055;
position: fixed;
top: 0;
left: 0;
z-index: 999;
}
#nprogress {
pointer-events: none;
}
#nprogress .bar {
background: #eb5055;
position: fixed;
z-index: 1031;
top: 0;
left: 0;
width: 100%;
height: 2px;
}
#nprogress .peg {
display: block;
position: absolute;
right: 0px;
width: 100px;
height: 100%;
box-shadow: 0 0 10px #eb5055, 0 0 5px #eb5055;
opacity: 1.0;
-webkit-transform: rotate(3deg) translate(0px, -4px);
-ms-transform: rotate(3deg) translate(0px, -4px);
transform: rotate(3deg) translate(0px, -4px);
}
#nprogress .spinner {
display: block;
position: fixed;
z-index: 1031;
top: 15px;
right: 15px;
}
#nprogress .spinner-icon {
width: 18px;
height: 18px;
box-sizing: border-box;
border: solid 2px transparent;
border-top-color: #eb5055;
border-left-color: #eb5055;
border-radius: 50%;
-webkit-animation: nprogress-spinner 400ms linear infinite;
animation: nprogress-spinner 400ms linear infinite;
}
.nprogress-custom-parent {
overflow: hidden;
position: relative;
}
.nprogress-custom-parent #nprogress .spinner,
.nprogress-custom-parent #nprogress .bar {
position: absolute;
}
@-webkit-keyframes nprogress-spinner {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes nprogress-spinner {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/** 导航栏 */
#mynavbar{
width: 100%;
height: 70px;
position: fixed;
display: block;
top: 0px;
z-index: 100;
background-color: white;
transition:0.5s ease-in-out;
box-shadow: 0 1px 5px rgba(0,0,0,.1);
}
#mynavbar_menu{
display: inline-block;
width: auto;
position: relative;
float: right;
text-align: right;
line-height: 68px;
}
#mynavbar_menu a{
padding: 0 15px;
font-size: 14px;
outline: 0;
color: #313131;
text-decoration: none;
}
#mynavbar_menu a:hover{
color: #eb5055 !important;
}
/** 头部 */
#myheader{
background-color: #2175bc!important;
margin-top:70px;
position: relative;
width: 100%;
height: 300px;
}
#myheader_bg{
width:100%;
height:300px;
background-position: center;
background-size: cover;
-webkit-filter: blur(3px);
-moz-filter: blur(3px);
filter: blur(3px);
position: absolute;
}
#myheader_cover{
position: relative;
margin-top: 70px;
width: 100%;
height: 300px;
background-color: rgba(0,0,0,.5);
box-shadow: 0 1px 5px rgba(0,0,0,.3);
}
#mypost_title{
position: relative;
top: 105pt;
margin: 0 auto;
padding: 30px 25px 20px;
max-width: 700px;
color: white;
transition:0.5s;
opacity: 0;
}
#mypost_title_e{
margin: 0;
padding: 5px 0 15px;
}
#mypost_title_e a{
border: 1px solid #6fa3ef;
border-radius: 15px;
background: #6fa3ef;
color: #fff;display: inline-block;
margin: 4px 8px 0 0;
padding: 0 15px;
letter-spacing: 0;
font-weight: 600;
font-size: 13px;outline: 0;text-decoration: none;
transition: 0.5s;
}
#mypost_title_e a:before{
content: "# "
}
#mypost_title_e a:hover{
background-color: white;
border: 1px solid white;
color:black;
}
#mypost_title_f a{
color: white;
}
#mypost_title_f a:hover{
text-decoration:none;
}
/** 右侧导航 */
#right_meun{
position: fixed;
z-index: 999;
top: 100px;
left: 69%;
display: none;
text-align: left;
border-left: 1px solid #ddd;
font-size: 12px;
}
#right_meun li{
list-style: none!important;
}
#right_meun a{
display: inline-table;
margin-left: 5px;
white-space: nowrap;
text-decoration: none;
color: #313131;
outline: 0;
}
#right_meun a:hover{
color: #eb5055;
}
#right_meun>li::before {
position: relative;
top: 0;
left: -4px;
display: inline-block;
width: 7px;
height: 7px;
content: '';
border-radius: 50%;
background-color: #eb5055;
}
/* 底部导航 */
#post-bottom-bar{
position: fixed;
right: 0;
bottom: 0;
left: 0;
z-index: 2;
height: 3pc;
border-top: 1px solid #e0e0e0;
background-color: #fff;
margin: 0;
padding: 0;
transition: 0.5s ease-in-out;
}
#post-bottom-bar a{
text-decoration: none!important;
}
.post-bottom-bar .bottom-bar-inner{
margin: 0 auto;
padding: 0 10px;
max-width: 900px;
}
.bottom-bar-items{
margin: 0 0 0 10px;
color: #313131;
font-size: 14px !important;
line-height: 3pc;float: left;
}
.post-bottom-bar{
margin: 0 0 0 10px;
color: #313131;
font-size: 14px;
line-height: 3pc;
}
.post-bottom-bar .social-share .bottom-bar-item {
padding: 4px;
}
.post-bottom-bar .bottom-bar-item.bottom-bar-facebook a {
background-color: #4267b2;
color: #fff;
}
.post-bottom-bar .social-share .bottom-bar-item a {
padding: 2px 10px;
border-radius: 15px;
}
.post-bottom-bar .bottom-bar-item a {
margin: 0;
padding: 9pt;
border: 0;
background: 0 0;
color: #313131;
font-size: 14px;
line-height: 3pc;
cursor: pointer;
}
.post-bottom-bar .bottom-bar-item.bottom-bar-twitter a {
background-color: #1b95e0;
color: #fff;
}
.post-bottom-bar .bottom-bar-item.bottom-bar-qrcode a {
background-color: #5f5f5f;
color: #fff;
}
.bottom-bar-item a:hover{
color: #eb5055;
}
/** MarkDown样式调整 */
.cnblogs-markdown .hljs{
font-size: 16px!important;
line-height: 2!important;
padding: 15px!important;
}
.cnblogs-markdown code{
background:rgb(238,240,244) none !important;
border:0px !important;
color: rgb(73,59,92) !important;
font-size: 16px!important;
}
.cnblogs-markdown h2{
font-weight: 500;
margin: 20px 0;
}
.cnblogs-markdown h2:before{
content: "#";
color: #eb5055;
position: relative;
top: 0;
left: -12px;
}
#cnblogs_post_body h2{
font-weight: 500;
margin: 20px 0;
}
#cnblogs_post_body h3{
font-size: 16px;
font-weight: bold;
line-height: 1.5;
margin: 10px 0;
}
.cnblogs-markdown h3:before{
content: "##";
color: #2175bc;
position: relative;
top: 0;
left: -8px;
}
.postBody blockquote, .postCon blockquote{
background-image: none;
border-left: 5px solid #DDDFE4;
background-color: #EEF0F4;
width: 100%;
padding: 6px 0 6px 25px;
}
blockquote{
border:0;
}
/* code加上行数 */
.cnblogs-markdown .syntaxhighlighter table td.code {
width:95% !important;
}
.cnblogs-markdown .syntaxhighlighter code {
font-family: "Consolas","Bitstream Vera Sans Mono","Courier New",Courier,monospace!important;
padding: 0 !important;
border-radius: 0 !important;
background-color: transparent !important;
}
.cnblogs-markdown .syntaxhighlighter code:before,
.cnblogs-markdown .syntaxhighlighter code:before {
letter-spacing: -0.5em;
}
/** 更改浏览器滚动条和选中字体背景颜色 */
::selection {
background-color: #eb5055;
color: #fff;
}
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-thumb {
min-height: 28px;
background-color: #c2c2c2;
background-clip: padding-box;
}
::-webkit-scrollbar-track-piece {
background-color: #fff;
}
*, :after, :before {
box-sizing: border-box;
}
页首Html代码
<!-- ............自定义首部代码开始............ -->
<!-- 自定义进度条 -->
<div id="myProgressBar"></div>
<!-- 自定义导航条 -->
<div id="mynavbar">
<div style="position:relative;width:65pc;height:70px;margin:0 auto;">
<a style="width: auto;height: 22px;margin-left: 25px;" href="https://www.cnblogs.com/summertime-wu">
<img style="width: auto;height: 22px;outline: 0;margin-top: 25px;" src="https://ww1.sinaimg.cn/large/0062YmUwgy1fthbsa7nmoj302g02ggle.jpg">
</a>
<div id="mynavbar_menu">
<a href="https://www.cnblogs.com/summertime-wu">首页</a>
<a href="https://msg.cnblogs.com/send/summertime-wu">联系</a>
<a href="https://github.com/Summertime-Wu">Github</a>
<a href="https://www.cnblogs.com/summertime-wu/p/9350833.html">友情链接</a>
<a href="https://www.cnblogs.com/">博客园</a>
<a href="https://www.cnblogs.com/summertime-wu/p/9350808.html">关于</a>
</div>
</div>
</div>
<!-- 自定义头部锚点 -->
<a name="mtop"></a>
<!-- 自定义头部 -->
<div id="myheader">
<!-- 背景图片 -->
<div id="myheader_bg"></div>
<!-- 遮罩层 -->
<div id="myheader_cover" title="点我点我">
<!-- 博客标题 -->
<div id="mypost_title">
<div id="mypost_title_h" style="font-weight: 500;font-size: 21px;">夏日浅笑、</div>
<div id="mypost_title_f">Published on 2017-09-02 11:31 in 暂未分类 with 夏日浅笑、</div>
<div id="mypost_title_e">
<a href="https://www.cnblogs.com/summertime-wu" style="">暂未分类</a>
</div>
</div>
</div>
</div>
<!-- ............自定义首部代码结束............ -->
页尾Html代码
<!-- ............自定义尾部代码开始............ -->
<div id="post-bottom-bar" class="post-bottom-bar">
<div class="bottom-bar-inner">
<!-- 左边 -->
<div class="bottom-bar-items social-share" style="float: left;">
<span class="bottom-bar-item bottom-bar-facebook">
<a id="bottom-d" href="javascript:void(0)" target="_blank">好文要赞</a>
</span>
<span class="bottom-bar-item bottom-bar-twitter">
<a id="bottom-g" href="javascript:void(0)"" target="_blank">关注我</a>
</span>
<span class="bottom-bar-item bottom-bar-qrcode">
<a id="bottom-s" href="javascript:void(0)" target="_blank">收藏该文</a>
</span>
</div>
<!-- 右边 -->
<div class="bottom-bar-items right" style="float: right;">
<span class="bottom-bar-item"><a id="mbottom-left" href="javascript:void(0);">←</a></span>
<span class="bottom-bar-item"><a id="mbottom-right" href="javascript:void(0);">→</a></span>
<span class="bottom-bar-item"><a href="#mfooter">↓</a></span>
<span class="bottom-bar-item"><a href="#mtop">↑</a></span>
</div>
</div>
</div>
<a name="mfooter"></a>
<!-- ............自定义尾部代码结束............ -->
<script type="text/javascript" src="https://unpkg.com/nprogress@0.2.0/nprogress.js"></script>
<script type="text/javascript">
/**
用来设置初始时需要执行的js
*/
$(document).ready(function(){
//加载顶部进度条
NProgress.start();
NProgress.done();
$("#myProgressBar").hide();
//设置背景图片地址
if ($("#head_bg_img").val()!=null && $("#head_bg_img").val()!="") {
$("#myheader_bg").css("background-image","url("+$("#head_bg_img").val()+")");
}else{
$("#myheader_bg").css("background-image","url(https://ww1.sinaimg.cn/large/0062YmUwgy1fthnpo4n7yj31hc0hrq8e.jpg)");
}
//标题
$("#mypost_title_h").html($("#cb_post_title_url").html());
//正文和标题淡入
$("#mainContent").css("opacity","1");
$("#mainContent").css("margin-top","0");
$("#mypost_title").css("opacity","1");
$("#mypost_title").css("top","75pt");
//更改iocn图标
var linkObject = document.createElement("link");
linkObject.rel = "shortcut icon";
linkObject.href = "https://ww1.sinaimg.cn/large/0062YmUwgy1fthbsa7nmoj302g02ggle.jpg";
document.getElementsByTagName("head")[0].appendChild(linkObject);
})
/**
设置ajax请求加载完所有数据时执行的js
*/
setTimeout(afterAjax,1000);
function afterAjax(){
//假如是首页
if (window.location.href == "https://www.cnblogs.com/summertime-wu"||
window.location.href == "https://www.cnblogs.com/summertime-wu/"||
window.location.href == "http://www.cnblogs.com/summertime-wu"||
window.location.href == "http://www.cnblogs.com/summertime-wu/") {
$("#mypost_title_f").html("");
$("#mypost_title_e").html("<a href='https://cnblogs.com/summertime-wu'>立德立言,无闻西东</a>");
}else{
var classification = null;
if ($("#BlogPostCategory").html()!=null && $("#BlogPostCategory").html()!= "") {
classification = $("#BlogPostCategory").html();
classification = classification.substring(3,classification.length);
}else{
classification = "<a href='https://cnblogs.com/summertime-wu'>暂未分类</a>";
}
var ftitle = "Published on "+ $("#post-date").html() + " in " + classification
+ " with <a href='https://cnblogs.com/summertime-wu'>夏日浅笑、</a>";
//设置副标题标题
$("#mypost_title_f").html(ftitle);
//设置标题分类按钮
$("#mypost_title_e").html(classification);
//设置底部导航条上一篇下一篇文章
var pnp = $("#post_next_prev a");
$("#mbottom-left").attr("href",pnp[1].href);
if (pnp.length>2) {
$("#mbottom-right").attr("href",pnp[3].href);
}
}
$("#myheader_bg").css("filter","blur(0px)");
}
/**
自定义的js方法
*/
//设置底部导航条点击事件
$("#bottom-g").click(function(){
follow('5dfde587-1816-e711-845c-ac853d9f53ac');
})
$("#bottom-s").click(function(){
AddToWz(cb_entryId);return false;
})
if (window.location.href == "https://www.cnblogs.com/summertime-wu"||
window.location.href == "https://www.cnblogs.com/summertime-wu/"||
window.location.href == "http://www.cnblogs.com/summertime-wu"||
window.location.href == "http://www.cnblogs.com/summertime-wu/") {
$("#bottom-d").html("友情链接");
$("#bottom-d").attr("href","https://www.cnblogs.com/summertime-wu/p/9350833.html");
}else{
var url = window.location.href;
var lastUrl = url.substring(url.lastIndexOf("/")+1);
var mpageId = lastUrl.substring(0,lastUrl.indexOf("."));
$("#bottom-d").click(function(){
DiggIt(mpageId,cb_blogId,1);
green_channel_success(this,'谢谢推荐!');
})
}
//通过滚动条控制导航条的展现隐藏
var oldScrollNum = 0;
window.onscroll = function(){
var t = document.documentElement.scrollTop || document.body.scrollTop;
//下滑
if (t>oldScrollNum) {
if (t>120) {
$("#mynavbar").css("margin-top","-70px");//顶部导航
$("#post-bottom-bar").css("bottom","-3pc");//底部导航
}
if (t>300) {
$("#right_meun").css("display","inline-block");//右侧导航
}
//上拉
}else{
//防止用a链接跳转也触发效果
if (oldScrollNum-t<20) {
$("#mynavbar").css("margin-top","0px");//顶部导航
$("#post-bottom-bar").css("bottom","0");//底部导航
}
if (t<=300) {
$("#right_meun").css("display","none");//右侧导航
}
}
oldScrollNum = t;
}
//背景大图点击模糊||清晰
$("#myheader_cover").click(function(){
if ($("#myheader_bg").css("filter") == "blur(3px)") {
$("#myheader_bg").css("filter","blur(0px)");
}else{
$("#myheader_bg").css("filter","blur(3px)");
}
})
</script>
<script type="text/javascript">
var setMyBlog = {
setCopyright: function() {
//设置版权信息,转载出处自动根据页面url生成
var info_str = '<p>作者:<a target="_blank">@gzdaijie</a><br>'+
'本文为作者原创,转载请注明出处:<a class="uri"></a></p><hr></hr>',
info = $(info_str),
info_a = info.find("a"),
url = window.location.href;
$(info_a[0]).attr("href","https://github.com/gzdaijie");
$(info_a[1]).attr("href",url).text(url);
$("#cnblogs_post_body").prepend(info);
},
setCodeRow: function(){
// 代码行号显示
var pre = $("pre.sourceCode"); //选中需要更改的部分
if(pre && pre.length){
pre.each(function() {
var item = $(this);
var lang = item.attr("class").split(" ")[1]; //判断高亮的语言
item.html(item.html().replace(/<[^>]+>/g,"")); //将<pre>标签中的html标签去掉
item.removeClass().addClass("brush: " + lang +";"); //根据语言添加笔刷
SyntaxHighlighter.all();
})
}
},
setAtarget: function() {
// 博客内的链接在新窗口打开
$("#cnblogs_post_body a").each(function(){
this.target = "_blank";
})
},
setContent: function() {
// 根据h2、h3标签自动生成目录
var captions_ori = $("#cnblogs_post_body h2"),
captions_ori2 = $("#cnblogs_post_body h3"),
captions = $("#cnblogs_post_body h2,#cnblogs_post_body h3").clone(),
content = $("<ul id='right_meun'></ul>");
$("#cnblogs_post_body").prepend(content.append(captions));
var index = -1,index2 = -1;
captions.replaceWith(function(){
var self = this;
if(self.tagName == "H2" || self.tagName == "h2"){
// 设置点击目录跳转
index += 1;
$('<a name="' + '_caption_' + index + '"></a>').insertBefore(captions_ori[index]);
return '<li id="'+index+'li"><a href="#_caption_' + index + '">' + self.innerHTML + '</a><ul></ul></li>';
} else {
// add by summertime-wu 增加h3链接跳转
index2 += 1;
$('<a name="' + '_caption' + index2 + '"></a>').insertBefore(captions_ori2[index2]);
$("#"+index+"li ul").append("<li><a href='#_caption" + index2 + "' style='color:#5f5f5f;'>" +self.innerHTML+"</a></li>");
return ;
}
});
},
runAll: function() {
/* 运行所有方法
* setAtarget() 博客园内标签新窗口打开
* setContent() 设置目录
* setCopyright() 设置版权信息
* setCodeRow() 代码行号显示
*/
this.setAtarget();
this.setContent();
// this.setCopyright();
this.setCodeRow();
}
}
setMyBlog.runAll();
</script>
实现过程
1、顶部加载条
我采用的是NProgress,这个我感觉非常好用。这是他们的官网,可以直接点四个方法查看效果。
点Download会跳到Github上去,上面有详情文档和CDN地址
我采取了最简单的方法
$(document).ready(function(){
NProgress.start();
NProgress.done();
}
效果是:
2、顶部导航条
顶部导航条要实现下滑隐藏,上滑加载。这个刚开始以为很难,没想到还是比较简单的。
百度找到监听滚动条事件的js方法
var oldScrollNum = 0;
window.onscroll = function(){
var t = document.documentElement.scrollTop || document.body.scrollTop;
//下滑
if (t>oldScrollNum) {
//上拉
}else{
}
oldScrollNum = t;
}
然后通过这个方法改变顶部导航条的margin-top的值达到展现隐藏的效果
最后给滚动条加上过渡属性transition: 0.5s ease-in-out;
,这样就达到了想要的效果。
ps:其实用CSS动画实现更流畅一点。
3、导航条上的关于和友情链接
这个其实就是写两篇随笔 ,因为随笔的地址是固定的所以可以写死。
4、背景图片模糊
这个css3提供了原生支持:filter: blur(3px);
原图:
duang!加了特效后:
5、为每篇文章单独的背景
这个得每篇文章里面加个隐藏域,value存背景图片的地址。
//设置背景图片地址
if ($("#head_bg_img").val()!=null && $("#head_bg_img").val()!="") {
$("#myheader_bg").css("background-image","url("+$("#head_bg_img").val()+")");
}else{
$("#myheader_bg").css("background-image","url(https://ww1.sinaimg.cn/large/0062YmUwgy1fthnpo4n7yj31hc0hrq8e.jpg)");
}
这里设成没有取到值就用默认的
6、修改markdown样式
这个就得呕心沥血的一点点调整了...
/** MarkDown样式调整 */
.cnblogs-markdown .hljs{
font-size: 16px!important;
line-height: 2!important;
padding: 15px!important;
}
.cnblogs-markdown code{
background:rgb(238,240,244) none !important;
border:0px !important;
color: rgb(73,59,92) !important;
font-size: 16px!important;
}
.cnblogs-markdown h2{
font-weight: 500;
margin: 20px 0;
}
.cnblogs-markdown h2:before{
content: "#";
color: #eb5055;
position: relative;
top: 0;
left: -12px;
}
#cnblogs_post_body h2{
font-weight: 500;
margin: 20px 0;
}
#cnblogs_post_body h3{
font-size: 16px;
font-weight: bold;
line-height: 1.5;
margin: 10px 0;
}
.cnblogs-markdown h3:before{
content: "##";
color: #2175bc;
position: relative;
top: 0;
left: -8px;
}
.postBody blockquote, .postCon blockquote{
background-image: none;
border-left: 5px solid #DDDFE4;
background-color: #EEF0F4;
width: 100%;
padding: 6px 0 6px 25px;
}
修改前:
修改后:
ps:感觉还是不太满意┑( ̄Д  ̄)┍
7、生成目录
这个采用了Github上的轮子:https://github.com/gzdaijie/cnblogs_markdown_optimize
然后我魔改了下。。
function() {
// 根据h2、h3标签自动生成目录
var captions_ori = $("#cnblogs_post_body h2"),
captions_ori2 = $("#cnblogs_post_body h3"),
captions = $("#cnblogs_post_body h2,#cnblogs_post_body h3").clone(),
content = $("<ul id='right_meun'></ul>");
$("#cnblogs_post_body").prepend(content.append(captions));
var index = -1,index2 = -1;
captions.replaceWith(function(){
var self = this;
if(self.tagName == "H2" || self.tagName == "h2"){
// 设置点击目录跳转
index += 1;
$('<a name="' + '_caption_' + index + '"></a>').insertBefore(captions_ori[index]);
return '<li id="'+index+'li"><a href="#_caption_' + index + '">' + self.innerHTML + '</a><ul></ul></li>';
} else {
// add by summertime-wu 增加h3链接跳转
index2 += 1;
$('<a name="' + '_caption' + index2 + '"></a>').insertBefore(captions_ori2[index2]);
$("#"+index+"li ul").append("<li><a href='#_caption" + index2 + "' style='color:#5f5f5f;'>" +self.innerHTML+"</a></li>");
return ;
}
});
}
eummm......看起来比较糟糕,但是能跑!!
效果:
8、底部导航条
这个实现和顶部的差不多,多的就是去顶尾部和上一篇下一篇的四个按钮,去顶部和去尾部用锚点很好实现,上一篇和下一篇则需要用js从页面上取值
然后赋给自定义的按钮。不过这里需要注意一点,由于这个上一篇和下一篇的链接博客园是采取ajax异步加载的方式,所以直接取链接可能导致取空值,需要设置一个定时器来取,我的是设置了1秒后再从页面上取。
/**
设置ajax请求加载完数据时执行的js
*/
setTimeout(afterAjax,1000);
function afterAjax(){
//取值
}
9、去除尾部广告
额,这个呢。实现so easy,找到对应广告元素的id,设置 display:none
就好了,但是有点心虚。毕竟博客园免费给我们提供的了地方写博客,然而我却......
10、适配手机
这个其实没有仔细处理,只是粗略的调整了下主体的大小,当浏览器宽度小于1000px时,将#main设为100%宽。
#main{
width: 40%;
background-color: white;
/*max-width: 700px;*/
}
@media screen and (max-width: 1000px) {
#main {width: 100%;}
}
效果:
11、针对首页优化
通过js获取当前url,然后匹配一下(其实这里可以优化一下,判断是否包含 summertime-wu
且后面没有下一级)
if (window.location.href == "https://www.cnblogs.com/summertime-wu"||
window.location.href == "https://www.cnblogs.com/summertime-wu/"||
window.location.href == "http://www.cnblogs.com/summertime-wu"||
window.location.href == "http://www.cnblogs.com/summertime-wu/") {
//针对性优化
}
是的,我现在才发现博客园既可以走http,也可以走https。。其实还可以这样:http://summertime-wu.cnblogs.com
12、访问统计
我这里采用的是http://www.flagcounter.com/,感觉挺不错的,不需要注册什么的,生成了html代码直接复制到侧边栏代码框里就可以用了。
不过由于我把侧边栏隐藏了,所以网页上看不到。正好我也不想让这个统计影响了整体的页面风格。
当然即使隐藏了还是能够正常统计的。假如自己需要查看则需要手动在控制台改下样式,让 #maincontent
缩小,#sidebar
展现。
总结
在这里郑重感谢Chakhsu Lau,是这位大神的博客给我提供了模仿的对象。ps:这位大神的博客是用他自己写的一个Typecho主题:pinghsu,使用了pjax技术,体验如丝滑般流畅。
由于自己是后端,不太会写前端,很多地方都不够优雅,甚至有点小蠢。但是,能跑!!
+△+
现在发现写前端原来这么爽,打开控制台你就是上帝了,很多东西都有现成的轮子,还非常简单好用。不过也发现前端不爽的地方,要适配各种分辨率和兼容各种浏览器。
自己同时是提需求的和实现需求的好矛盾啊啊啊。不过我最后坚定了一个想法,无论多难都要实现,打磨好每一个细节
ˋ︿ˊ
源码稍后会放在 Github上,如有做得不好的地方,请不吝赐教,假如觉得不错的话给个star吧
˙▽˙
本文作者:夏日浅笑、
原文地址:https://www.cnblogs.com/summertime-wu/p/9356736.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。