You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 lines
4.8 KiB

(function ($) {
$.fn.OTKRandomElement = function (options) {
var defaults = {
intShow: 8,
ChildType: 'div',
Hide: true,
HideTag: 'class',
HideCondition: 'displayNone',
Fixed: true,
FixedTag: 'randomfixed',
FixedCondition: 'Y',
ArrRandom: true,
SetClass: false,
ClassName: 'box4n',
ClassCondition: '4n',
RandomMs: 2500
};
var settings = $.extend(defaults, options);
var guid = (function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function () {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
})();
var obj = this;
var intObjLength = $(obj).children(settings.ChildType).length;
var strRandomSortGUID = 'RandomSort' + guid().replace(/\-/g, '');
eval('var ' + strRandomSortGUID);
var ArrSort = GetSortArray(obj, settings.ChildType);
eval(strRandomSortGUID + '=ArrSort');
SetShowObj(obj, settings.ChildType);
var timer = setInterval(function () { SetCommodityLoopTest(obj, settings.ChildType, strRandomSortGUID, intObjLength) }, settings.RandomMs); //設定輪播效果
function SetCommodityLoopTest(obj, ChildType, strRandomSortGUID, intObjLength) {
var intRandomSort = eval(strRandomSortGUID + '[0]');
var intShowThis = settings.intShow + 1;
var row = $(obj).children(ChildType + ':nth-child(' + intShowThis + ')')
var MoveToRow = $(obj).children(ChildType + ':nth-child(' + intRandomSort + ')')
MoveToRow.hide();
row.insertBefore(MoveToRow); //放在新的值的前面
var LastRow = $(obj).children(ChildType + ':nth-child(' + intObjLength + ')')
MoveToRow.insertAfter(LastRow); //放在新的值的前面
if (row.attr(settings.FixedTag) != settings.FixedCondition || !settings.Fixed) {
eval(strRandomSortGUID + '.push(' + strRandomSortGUID + '[0])');
}
eval(strRandomSortGUID + '.splice(0,1)');
SetShowObj(obj, ChildType);
SetDivHoverEventTwo(obj, ChildType, strRandomSortGUID);
}
function GetSortArray(obj, ChildType) {
var ArrSort = new Array;
$(obj).children(ChildType).each(function () {
if ($(this).index() < settings.intShow) {
if ($(this).attr(settings.FixedTag) != settings.FixedCondition || !settings.Fixed) {
ArrSort.push($(this).index() + 1);
}
}
})
if (settings.ArrRandom && ArrSort.length > 1) {
ArrSort = ArrSort.sort(shuffle);
}
return ArrSort;
}
function SetShowObj(obj, ChildType) {
$(obj).children(ChildType).each(function () {
if ($(this).index() < settings.intShow) {
if ($(this).attr(settings.HideTag) != settings.HideCondition || !settings.Hide) {
$(this).show();
}
}
else {
$(this).hide();
}
})
if (settings.SetClass) {
$(obj).children(ChildType).removeClass(settings.ClassName);
$(obj).children(ChildType + settings.ClassCondition).addClass(settings.ClassName);
}
}
function SetDivHoverEventTwo(obj, ChildType, strRandomSortGUID) {
$(obj).children(ChildType).each(function () {
if ($(this).attr(settings.FixedTag) != settings.FixedCondition || !settings.Fixed) {
$(this).hover(function () {
var intIdxNum = jQuery.inArray($(this).index() + 1, eval(strRandomSortGUID));
if (intIdxNum >= 0) { //有找到再刪除!
eval(strRandomSortGUID + '.splice(' + intIdxNum + ',1)');
}
});
$(this).bind('mouseleave', function () {
var intIdxNum = $(this).index() + 1;
if ($(this).attr(settings.FixedTag) != settings.FixedCondition || !settings.Fixed) {
eval(strRandomSortGUID + '.push(' + intIdxNum + ')');
}
});
}
})
}
function shuffle(a, b) {
var num = Math.random() > 0.5 ? -1 : 1;
return num;
}
}
})(jQuery);