(function(){
function EventUtil(){// add eventHandler to element . by guop
this.addHandler = function(element,type,handler) {
if (element.addEventListener) {
element.addEventListener(type,handler,false);
} else if (element.attachEvent) {
element.attachEvent("on"+type,handler);
} else {
element["on"+type] = handler;
}
},
this.removeHandler = function (element,type,handler) {// delete eventHandler to element . by guop
if (element.removeEventListener) {
element.removeEventListener(type,handler,false);
} else if (element.detachEvent) {
element.detachEvent("on"+type,handler);
} else {
element["on"+type] = null;
}
}
}
this.getEvent = function (event) {// return event object to IE and other
return event ? event : window.event;
}
this.getTarget = function(event) {// return target object to IE and other
return event.target || event.srcElement;
}
this.preventDefault = function (event) {//prevent default to from IE and other
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
this.stopPropagation = function(event) {// stop propagation event propagation to IE and other
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
this.getCharCode = function (event) {// return keyboard keypress charcode to IE and ohther
if (typeof event.charCode == "number") {
return event.charCode;
} else {
return event.keyCode;
}
}
window.EventUtil = new EventUtil();
})();
|
function UUID() {
this.id = this.createUUID();
}
// When asked what this Object is, lie and return it's value
UUID.prototype.valueOf = function() {
return this.id;
};
UUID.prototype.toString = function() {
return this.id;
};
//
// INSTANCE SPECIFIC METHODS
//
UUID.prototype.createUUID = function() {
//
// Loose interpretation of the specification DCE 1.1: Remote Procedure Call
// since JavaScript doesn't allow access to internal systems, the last 48
// bits
// of the node section is made up using a series of random numbers (6 octets
// long).
//
var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
var dc = new Date();
var t = dc.getTime() - dg.getTime();
var tl = UUID.getIntegerBits(t, 0, 31);
var tm = UUID.getIntegerBits(t, 32, 47);
var thv = UUID.getIntegerBits(t, 48, 59) + '1'; // version 1, security
// version is 2
var csar = UUID.getIntegerBits(UUID.rand(4095), 0, 7);
var csl = UUID.getIntegerBits(UUID.rand(4095), 0, 7);
// since detection of anything about the machine/browser is far to buggy,
// include some more random numbers here
// if NIC or an IP can be obtained reliably, that should be put in
// here instead.
var n = UUID.getIntegerBits(UUID.rand(8191), 0, 7)
+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)
+ UUID.getIntegerBits(UUID.rand(8191), 0, 7)
+ UUID.getIntegerBits(UUID.rand(8191), 8, 15)
+ UUID.getIntegerBits(UUID.rand(8191), 0, 15); // this last number
// is two octets
// long
return tl + tm + thv + csar + csl + n;
};
// Pull out only certain bits from a very large integer, used to get the time
// code information for the first part of a UUID. Will return zero's if there
// aren't enough bits to shift where it needs to.
UUID.getIntegerBits = function(val, start, end) {
var base16 = UUID.returnBase(val, 16);
var quadArray = new Array();
var quadString = '';
var i = 0;
for (i = 0; i < base16.length; i++) {
quadArray.push(base16.substring(i, i + 1));
}
for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
if (!quadArray[i] || quadArray[i] == '')
quadString += '0';
else
quadString += quadArray[i];
}
return quadString;
};
// Replaced from the original function to leverage the built in methods in
// JavaScript. Thanks to Robert Kieffer for pointing this one out
UUID.returnBase = function(number, base) {
return (number).toString(base).toUpperCase();
};
// pick a random number within a range of numbers
// int b rand(int a); where 0 <= b <= a
UUID.rand = function(max) {
return Math.floor(Math.random() * (max + 1));
};
|
//js 代码
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
var CurrentStyle = function(element){
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
var Bind = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function() {
return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
}
}
var Tween = {
Quart: {
easeOut: function(t,b,c,d){
return -c * ((t=t/d-1)*t*t*t - 1) + b;
}
},
Back: {
easeOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
},
Bounce: {
easeOut: function(t,b,c,d){
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
} else {
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
}
}
}
}
//容器对象,滑动对象,切换数量
var SlideTrans = function(container, slider, count, options) {
this._slider = $(slider);
this._container = $(container);//容器对象
this._timer = null;//定时器
this._count = Math.abs(count);//切换数量
this._target = 0;//目标值
this._t = this._b = this._c = 0;//tween参数
this.Index = 0;//当前索引
this.SetOptions(options);
this.Auto = !!this.options.Auto;
this.Duration = Math.abs(this.options.Duration);
this.Time = Math.abs(this.options.Time);
this.Pause = Math.abs(this.options.Pause);
this.Tween = this.options.Tween;
this.onStart = this.options.onStart;
this.onFinish = this.options.onFinish;
var bVertical = !!this.options.Vertical;
this._css = bVertical ? "top" : "left";//方向
//样式设置
var p = CurrentStyle(this._container).position;
p == "relative" || p == "absolute" || (this._container.style.position = "relative");
this._container.style.overflow = "hidden";
this._slider.style.position = "absolute";
this.Change = this.options.Change ? this.options.Change :
this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;
};
SlideTrans.prototype = {
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
Vertical: true,//是否垂直方向(方向不能改)
Auto: true,//是否自动
Change: 0,//改变量
Duration: 50,//滑动持续时间
Time: 10,//滑动延时
Pause: 4000,//停顿时间(Auto为true时有效)
onStart: function(){},//开始转换时执行
onFinish: function(){},//完成转换时执行
Tween: Tween.Quart.easeOut//tween算子
};
Extend(this.options, options || {});
},
//开始切换
Run: function(index) {
//修正index
index == undefined && (index = this.Index);
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
//设置参数
this._target = -Math.abs(this.Change) * (this.Index = index);
this._t = 0;
this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);
this._c = this._target - this._b;
this.onStart();
this.Move();
},
//移动
Move: function() {
clearTimeout(this._timer);
//未到达目标继续移动否则进行下一次滑动
if (this._c && this._t < this.Duration) {
this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));
this._timer = setTimeout(Bind(this, this.Move), this.Time);
}else{
this.MoveTo(this._target);
this.Auto && (this._timer = setTimeout(Bind(this, this.Next), this.Pause));
}
},
//移动到
MoveTo: function(i) {
this._slider.style[this._css] = i + "px";
},
//下一个
Next: function() {
this.Run(++this.Index);
},
//上一个
Previous: function() {
this.Run(--this.Index);
},
//停止
Stop: function() {
clearTimeout(this._timer); this.MoveTo(this._target);
}
};
//html页面代码
<!DOCTYPE HTML>
<head>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<title>京东首页 – 触屏版测试</title>
<script src="/vancl/picMove.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
function init(){
//获得商品图片个数 测试值为4
var picNum = 4;
//获得图片宽度与高度
var widt = 304;
var heigh = 123;
var _left = (widt - picNum * 16)/2 - 15 +"px";
jQuery(".pic-num").css("left",_left).css("visibility","visible");
}
</script>
<style type="text/css">
/*图片尺寸修改后 必须改此尺寸 多平台时需要适配*/
.container, .container img{
width:304px;
height:123px;
margin:0px auto;
}
/* 广告图片 */
.index-ads {
padding: 0px;
}
/* 需要根据图片宽度以及图片数量计算位置(后期要优化) */
.pic-num {
position: absolute;
left : 100px;
bottom:0px;
margin: 0px;
visibility:hidden;
}
.pic-num li {
float: left;
list-style-type: disc;
text-align: center;
cursor: pointer;
margin: 0px;
width:15px;
}
.pic-num li.on {
color:#ff22ee;
}
.pic-num-div{
height:20px;
width:300px;
text-align:center;
visibility:visible;
}
</style>
</head>
<body onload="init()">
<div class="index-ads">
<div class="container" id="idContainer2" ontouchmove="touchMove(event);" ontouchend="touchEnd(event);">
<table id="idSlider2" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href="activity.html"><img src="/vancl/images/advert2.jpg"></a></td>
<td><a href="activity.html"><img src="/vancl/images/advert.jpg"></a></td>
<td><a href="activity.html"><img src="/vancl/images/advert3.jpg"></a></td>
<td><a href="activity.html"><img src="/vancl/images/advert2.jpg"></a></td>
</tr>
</table>
<ul class="pic-num" id="idNum"></ul>
</div>
<!-- div class="pic-num-div">
<span style="position:absolute;text-align:center;background-color:#ffee11;">
<ul class="pic-num" id="idNum"></ul>
</span>
</div -->
</div>
</body>
</html>
<script type="text/javascript">
jQuery.noConflict();
var forEach = function(array, callback){
for (var i = 0, len = array.length; i < len; i++) { callback.call(this, array[i], i); }
}
var st = new SlideTrans("idContainer2", "idSlider2", 4, { Vertical: false }); //图片数量更改后需更改此数值
var nums = [];
//插入数字
for(var i = 0, n = st._count - 1; i <= n;i++){
//(nums[i] = $("idNum").appendChild(document.createElement("li"))).innerHTML = ++i;
nums[i] = $("idNum").appendChild(document.createElement("li"));
}
//设置按钮样式
st.onStart = function(){
forEach(nums, function(o, i){ o.className = st.Index == i ? "on" : ""; })
}
//触屏滑动事件
var touch = [];
function touchMove(event){
var touches = event.touches;
var fristTouch = touches[0];
touch.push(fristTouch.pageX);
event.preventDefault();
}
//触屏 离开屏幕事件
function touchEnd(event){
var touches = event.touches;
var fristTouch = touches[0];
if (touch != undefined && touch.length > 1) {
var start = touch.shift();
var end = touch.pop();
if (start >= end) {
st.Next();
} else {
st.Previous();
}
touch = [];
event.preventDefault();
}
}
st.Run();
</script>
|