无论是Zblog网站还是wordpress,首页幻灯片功能几乎成了标配,果果另一个网站正在使用的主题是一款收费老主题,没有添加幻灯片功能,虽说不影响使用,可和别人的比起来,总感觉自己的少了点什么。起初用的是应用中心的免费插件,已经没人维护,而且插件这东西,你懂得,能少用就少用,于是就从网上找了教程,且成功给zblog网站添加了幻灯片功能,把这篇教程分享给各位。
1、打包下方代码,新建slides.js文件,上传至主题根目录的script文件夹下。
(function(c,I,B){c.fn.responsiveSlides=function(l){var a=c.extend({auto:!0,speed:500,timeout:4E3,pager:!1,nav:!1,random:!1,pause:!1,pauseControls:!0,prevText:"<i class='fa fa-angle-left'></i>",nextText:"<i class='fa fa-angle-right'></i>",maxwidth:"",navContainer:"",manualControls:"",namespace:"rslides",before:c.noop,after:c.noop},l);return this.each(function(){B++;var f=c(this),s,r,t,m,p,q,n=0,e=f.children(),C=e.size(),h=parseFloat(a.speed),D=parseFloat(a.timeout),u=parseFloat(a.maxwidth),g=a.namespace,d=g+B,E=g+"_nav "+d+"_nav",v=g+"_here",j=d+"_on", w=d+"_s",k=c("<ul class='"+g+"_tabs "+d+"_tabs' />"),x={"float":"left",position:"relative",opacity:1,zIndex:2},y={"float":"none",position:"absolute",opacity:0,zIndex:1},F=function(){var b=(document.body||document.documentElement).style,a="transition";if("string"===typeof b[a])return!0;s=["Moz","Webkit","Khtml","O","ms"];var a=a.charAt(0).toUpperCase()+a.substr(1),c;for(c=0;c<s.length;c++)if("string"===typeof b[s[c]+a])return!0;return!1}(),z=function(b){a.before(b);F?(e.removeClass(j).css(y).eq(b).addClass(j).css(x), n=b,setTimeout(function(){a.after(b)},h)):e.stop().fadeOut(h,function(){c(this).removeClass(j).css(y).css("opacity",1)}).eq(b).fadeIn(h,function(){c(this).addClass(j).css(x);a.after(b);n=b})};a.random&&(e.sort(function(){return Math.round(Math.random())-0.5}),f.empty().append(e));e.each(function(a){this.id=w+a});f.addClass(g+" "+d);l&&l.maxwidth&&f.css("max-width",u);e.hide().css(y).eq(0).addClass(j).css(x).show();F&&e.show().css({"-webkit-transition":"opacity "+h+"ms ease-in-out","-moz-transition":"opacity "+ h+"ms ease-in-out","-o-transition":"opacity "+h+"ms ease-in-out",transition:"opacity "+h+"ms ease-in-out"});if(1<e.size()){if(D<h+100)return;if(a.pager&&!a.manualControls){var A=[];e.each(function(a){a+=1;A+="<li><a href='#' class='" + w + a + "'></a></li>"});k.append(A);l.navContainer?c(a.navContainer).append(k):f.after(k)}a.manualControls&&(k=c(a.manualControls),k.addClass(g+"_tabs "+d+"_tabs"));(a.pager||a.manualControls)&&k.find("li").each(function(a){c(this).addClass(w+(a+1))});if(a.pager||a.manualControls)q= k.find("a"),r=function(a){q.closest("li").removeClass(v).eq(a).addClass(v)};a.auto&&(t=function(){p=setInterval(function(){e.stop(!0,!0);var b=n+1<C?n+1:0;(a.pager||a.manualControls)&&r(b);z(b)},D)},t());m=function(){a.auto&&(clearInterval(p),t())};a.pause&&f.hover(function(){clearInterval(p)},function(){m()});if(a.pager||a.manualControls)q.bind("mouseover",function(b){b.preventDefault();a.pauseControls||m();b=q.index(this);n===b||c("."+j).queue("fx").length||(r(b),z(b))}).eq(0).closest("li").addClass(v), a.pauseControls&&q.hover(function(){clearInterval(p)},function(){m()});if(a.nav){g="<a href='#' class='"+E+" prev'>"+a.prevText+"</a><a href='#' class='"+E+" next'>"+a.nextText+"</a>";l.navContainer?c(a.navContainer).append(g):f.after(g);var d=c("."+d+"_nav"),G=d.filter(".prev");d.bind("click",function(b){b.preventDefault();b=c("."+j);if(!b.queue("fx").length){var d=e.index(b);b=d-1;d=d+1<C?n+1:0;z(c(this)[0]===G[0]?b:d);if(a.pager||a.manualControls)r(c(this)[0]===G[0]?b:d);a.pauseControls||m()}}); a.pauseControls&&d.hover(function(){clearInterval(p)},function(){m()})}}if("undefined"===typeof document.body.style.maxWidth&&l.maxwidth){var H=function(){f.css("width","100%");f.width()>u&&f.css("width",u)};H();c(I).bind("resize",function(){H()})}})}})(jQuery,this,0); $(document).ready(function(){ $("#slider").responsiveSlides({ auto: true, pager: true, nav: true, speed: 800, maxwidth: 1200 }); $("#slide").responsiveSlides({ auto: false, pager: true, nav: true, speed: 800, maxwidth: 1200 }); });
2、在首页调用的css代码中,添加如下css样式。
#slideshow { position:relative; margin:0 0 10px; box-shadow:0 1px 1px rgba(0,0,0,.1) } #gallery:hover .rslides_nav,#slideshow:hover .rslides_nav { display:block } .rslides_nav .prev { z-index:9999 } @-webkit-keyframes fadeInUp { 0% { opacity:0; -webkit-transform:translateY(20px); transform:translateY(20px) } to { opacity:1; -webkit-transform:translateY(0); transform:translateY(0) } }@keyframes fadeInUp { 0% { opacity:0; -webkit-transform:translateY(20px); transform:translateY(20px); -ms-transform:translateY(20px) } to { opacity:1; -webkit-transform:translateY(0); transform:translateY(0); -ms-transform:translateY(0) } }.fadeInUp { -webkit-animation-name:fadeInUp; animation-name:fadeInUp } .cat-dt .entry-content,.dengji,.entry-meta .edit-link,.p-content,.plxiaoshi,.slider-caption { display:none } .slider-caption { position:absolute; bottom:0; left:0; z-index:2; display:block; padding:8px 15px; width:100%; background:-webkit-linear-gradient(top,transparent 0,rgba(0,0,0,.6) 100%); background:linear-gradient(top,transparent 0,rgba(0,0,0,.6) 100%); color:#fff; font-size:20px; line-height:30px; animation:fade-in; animation-duration:.5s; -webkit-animation:fade-in .5s } .rslides { position:relative; overflow:hidden; width:100% } .rslides li { position:absolute; top:0; left:0; display:none; width:100%; -webkit-backface-visibility:hidden } .rslides li:first-child { position:relative; float:left; display:block } .rslides img { float:left; display:block; width:100%; height:auto; border-radius:2px } .rslides_tabs { position:absolute; bottom:0; z-index:2; margin:0 auto; padding:10px 0; width:100%; max-width:100%; text-align:right } .rslides_tabs li { float:none; display:inline; margin-right:5px } .rslides_tabs a { display:inline; padding:2px; width:4px; height:4px; border:1px solid #fff; border-radius:4px; background:#fff; color:#555; font-size:0; line-height:4px } .rslides_tabs li:first-child { margin-left:0 } .rslides_tabs .rslides_here a { padding:4px; width:8px; height:8px; border:1px solid #c01e22; border-radius:8px; background:#c01e22; color:#fff; line-height:8px } #gallery .rslides_nav { top:45% } #gallery .rslides_tabs { bottom:30px; padding:10px; text-align:right } .rslides_nav { position:absolute; top:56%; left:0; z-index:3; display:none; overflow:hidden; margin-top:-45px; width:50px; height:50px; border-radius:2px; background:#000!important; background:0 0 rgba(0,0,0,.5); color:#fff!important; text-align:center; text-decoration:none; font-size:50px; line-height:45px; opacity:.5; filter:alpha(opacity=50) } #gallery:hover .rslides_nav,#slideshow:hover .rslides_nav { display:block } .rslides_nav .prev { z-index:9999 } .rslides_nav.next { right:0; left:auto }
3、主题header文件中,添加js引用代码:
<script src="{$host}zb_users/theme/{$theme}/script/slides.js" type="text/javascript"></script>
4、在需要添加幻灯片功能的位置,按照下方格式添加幻灯片。
<div id="slideshow" class="wow fadeInUp" data-wow-delay="0.3s"> <ul id="slider" class="rslides" > <li><a target="_blank" href="https://www.qdxgfj.com/"><img src="https://www.qdxgfj.com/" alt="为Zblog网站添加幻灯片功能的教程" ></a><p class="slider-caption">为Zblog网站添加幻灯片功能的教程</p></li> </ul> </div>
每复制一行li代码,就相当于多添加一个幻灯片图片。
以上这种方法是利用了ResponsiveSlides这个幻灯片插件,为Zblog网站添加幻灯片功能,有不明白的地方也可以去插件官网阅读插件使用说明或者去百度搜索更多教程。