2011年8月21日星期日

非插件版实现wp-auto-top

From:飞鱼博客http://blog.e-520.com.cn/archives/2088.html

刚从园子博客中学了WordPress左侧滚动导航栏效果,我的博客中也用上了。有必要将操作过程作个记录。

左侧滚动条效果说明图

这里用WordPress的inove主题为例,其它博客实现原理类似。

(代码看懂前提需掌握Html、Css、JavaScript、Php、jQuery。)

1.在主题文件footer.php中添加如下代码:

     <!-- shangxia START -->     <div id="shangxia"><div id="shang"></div><div id="comt"></div><div id="xia"></div></div>     <!-- shangxia END --> 

注意要加在body结束标签之前。

2.在主题文件header.php的head区域添加如下代码:

 <?php if (is_single()) { ?> <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/shangxia-single.css" type="text/css" /> <?php } else { ?> <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/shangxia-other.css" type="text/css" /> <?php } ?> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> <!-- jQuery(document).ready(function($){ var s= $('#shangxia').offset().top;$(window).scroll(function (){$("#shangxia").animate({top : $(window).scrollTop() + s + "px" },{queue:false,duration:500});}); $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body'); $('#shang').click(function(){$body.animate({scrollTop: '0px'}, 400);}); $('#xia').click(function(){$body.animate({scrollTop:$('#footer').offset().top}, 800);}); $('#comt').click(function(){$body.animate({scrollTop:$('#comments').offset().top}, 800);}); }); --> </script> 

说明:这步通过条件语句判断加载不同的CSS文件,然后是调用了jQuery类库,并添加效果代码。其中<?php bloginfo(’stylesheet_directory’); ?>为主题对应的目录。

3.在主题目录中创建shangxia-single.css文件,内容为:

 /* shangxia-single START */ #shangxia{position:absolute;top:40%;left:50%;margin-left:-503px;display:block;} #shang{background:url(img/shangxia.gif) no-repeat;position:relative;cursor:pointer;height:42px;width:32px;margin:10px 0;} #comt{background:url(img/shangxia.gif) no-repeat center -45px;position:relative;cursor:pointer;height:32px;width:32px;margin:10px 0;} #xia{background:url(img/shangxia.gif) no-repeat center -78px;position:relative;cursor:pointer;height:42px;width:32px;margin:10px 0;} /* shangxia-single END */ 

说明:其中margin-left:-503px中的值,需要根据自己网站的显示效果自行调试,负值越大越靠近左侧屏幕边缘。

4.在主题目录中创建shangxia-other.css文件,内容为:

 /* shangxia-other START */ #shangxia{position:absolute;top:40%;left:50%;margin-left:-503px;display:block;} #shang{background:url(img/shangxia.gif) no-repeat;position:relative;cursor:pointer;height:42px;width:32px;margin:10px 0;} #xia{background:url(img/shangxia.gif) no-repeat center -78px;position:relative;cursor:pointer;height:42px;width:32px;margin:10px 0;} /* shangxia-other END */ 

5.准备好图片素材,到主题的img目录中:

shangxia

这个文件尺寸为32×119,其必须和第3、4步的css代码相配合才行。

6.最后,上传素材和修改的文件,并微调导航栏的位置。

转载请注明来源: 小仓のblog
本文标题: 非插件版实现wp-auto-top
本文链接地址:http://www.xxcang.com/1053.html

一键转载:

1.2K

非特殊声明本站所有文章,图片,视频全部来自网络,如有侵权,请通知本站。


http://www.xxcang.com/1053.html

没有评论:

发表评论