本文介绍一组适用于在Wordpress侧栏放置的图片广告轮播(js)代码:可自动播放,同时响应访客的点击 1.样式表 - /*SidebarTabsAd*/
- #cwpad_box{width:100%;text-align:center}
- #cwpad_box ul, #cwpad_box dd, #cwpad_box tt{
- margin:0px;
- padding:0px;
- float:left;
- list-style: none;
- }
- #cwpad_box{
- width:313px;/*250px+(n-1)*21px*/
- height:250px;
- border-left: 1px solid #740a20;
- }
- #cwpad_box dd{
- width:21px;
- height:250px;
- overflow:hidden;
- position:relative;
- }
- #cwpad_box dd.hove{
- width:250px;
- text-align:rightright;
- }
- #cwpad_box dd tt{
- width:20px;
- height:250px;
- top:0px;
- left:0px;
- color:#fff;
- cursor:pointer;
- text-align:center;
- padding:20px 0 0 0;
- background:#b9000d;
- position:absolute;
- border-right:1px solid #740a20;
- }
- #cwpad_box dd tt.hove{
- background:#620317;
- }
在上面的样式表中,假设放置的是四个250px*250px的图片广告,所以250+(4-1)*21=313px。 2.javascript代码 - <script type="text/javascript">
- function myAddEvent(obj, sEvent, fn){
- return obj.attachEvent ? obj.attachEvent('on' + sEvent, fn) : obj.addEventListener(sEvent, fn, false);
- }
- function Class(oParent, sClass){
- var aElem = oParent.getElementsByTagName('*');
- var aClass = [];
- var i = 0;
- for(i=0;i<aElem.length;i++)if(aElem[i].className == sClass)aClass.push(aElem[i]);
- return aClass;
- };
- function css(obj, attr, value){
- if(arguments.length == 2){
- var style = obj.style,
- currentStyle = obj.currentStyle;
- if(typeof attr === 'string')return currentStyle ? currentStyle[attr] : getComputedStyle(obj, false)[attr];
- for(var propName in attr)propName == 'opacity' ? (style.filter = "alpha(opacity=" + attr[propName] + ")", style.opacity = attr[propName] / 100) : style[propName] = attr[propName];
- }else if(arguments.length == 3){
- switch(attr){
- case "width":
- case "height":
- case "paddingTop":
- case "paddingRight":
- case "paddingBottom":
- case "paddingLeft":
- case "top":
- case "right":
- case "bottom":
- case "left":
- case "marginTop":
- case "marginRigth":
- case "marginBottom":
- case "marginLeft":
- obj.style[attr] = value + "px";
- break;
- case "opacity":
- obj.style.filter = "alpha(opacity=" + value + ")";
- obj.style.opacity = value / 100;
- break;
- default:
- obj.style[attr] = value
- }
- }
- };
- function extend(destination, source){
- for (var propName in source) destination[propName] = source[propName];
- return destination
- };
- function doMove(obj, json, fnEnd){
- clearInterval(obj.timer);
- obj.iSpeed = 0;
- fnEnd = extend({
- type: "buffer",
- callback: function() {}
- }, fnEnd);
- obj.timer = setInterval(function(){
- var iCur = 0,
- iStop = true;
- for(var propName in json){
- iCur = parseFloat(css(obj, propName));
- propName == 'opacity' && (iCur = Math.round(iCur * 100));
- switch(fnEnd.type){
- case 'buffer':
- obj.iSpeed = (json[propName] - iCur) / 5;
- obj.iSpeed = obj.iSpeed > 0 ? Math.ceil(obj.iSpeed) : Math.floor(obj.iSpeed);
- json[propName] == iCur || (iStop = false, css(obj, propName, iCur + obj.iSpeed));
- break;
- case 'elasticity':
- obj.iSpeed += (json[propName] - iCur) / 5;
- obj.iSpeed *= 0.75;
- Math.abs(json[propName] - iCur) <= 1 && Math.abs(obj.iSpeed) <= 1 ? css(obj, propName, json[propName]) : css(obj, propName, json[propName]) || (iStop = false, css(obj, propName, iCur + obj.iSpeed));
- break;
- case 'accelerate':
- obj.iSpeed = obj.iSpeed + 5;
- iCur >= json[propName] ? css(obj, propName, json[propName]) : css(obj, propName, json[propName]) || (iStop = false, css(obj, propName, iCur + obj.iSpeed));
- break;
- }
- }
- if(iStop){
- clearInterval(obj.timer);
- obj.timer = null;
- obj.iSpeed = 0;
- fnEnd.callback();
- }
- },30);
- };
-
- window.onload = function(){
- var oBox = document.getElementById('cwpad_box')
- var aSpan = document.getElementsByTagName('tt');
- var aLi = document.getElementsByTagName('dd');
- var playtime = null;
- var iNow = 0;
- for(i=0;i<aSpan.length;i++){
- aSpan[i].index = i;
- aSpan[i].onclick = function(){
- for(var len=aLi.length,i=0;i<len;i++)doMove(aLi[i], {width:21});
- for(var len=aSpan.length,i=0;i<len;i++)aSpan[i].className = '';
- this.className = 'hove';
- doMove(this.parentNode, {width:250});
- iNow = this.index;
- };
- }
- playtime = setInterval(tab,3500);
- oBox.onmouseover = function(){
- clearInterval(playtime);
- }
- oBox.onmouseout = function(){
- playtime = setInterval(tab,3500);
- }
- function tab(){
- iNow == aLi.length-1 ? iNow = 0 : iNow++;
- aSpan[iNow].onclick();
- }
- };
- </script>
3.示例的HTML代码 - <div id="cwpad_box">
- <ul>
- <dd class="hove">
- <tt class="hove">网站设计及培训</tt>
- <a href="http://suoling.net/" target=_blank title="网站设计及培训"><img src="http://fakeimg.pl/250x250/?text=Hello,A!"></a>
- </dd>
- <dd>
- <tt>网站优化及培训</tt>
- <a href="http://suoling.net/" target=_blank title="网站优化及培训"><img src="http://fakeimg.pl/250x250/?text=Hello,B!"></a>
- </dd>
- <dd>
- <tt>网站诊断</tt>
- <a href="http://suoling.net/" target=_blank title="网站诊断"><img src="http://fakeimg.pl/250x250/?text=Hello,C!"></a>
- </dd>
- <dd>
- <tt>电子商务培训</tt>
- <a href="http://suoling.net/" target=_blank title="电子商务培训"><img src="http://fakeimg.pl/250x250/?text=Hello,D!"></a>
- </dd>
- </ul>
- </div>
使用的是索凌网络前面介绍过的"假图"生成网站fakeimg.pl生成的"假图"。 这组代码的一个显著缺点是:不适合移动设备显示。
|