不要让广告彼此靠近 jquery
don't let adverts appear near each other jquery
jQuery(document).ready(function(){
var insertionTemplate = jQuery(".hiddenAdBox").eq(0).html(),
insertionTarget = jQuery('ul'),
insertionTargetChildren = insertionTarget.find('li'),
insertionFrequency = 2;
var random;
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
insertionTargetChildren.eq(random).after(insertionTemplate);
}
});
我有项目列表,还需要在刷新页面时随机显示广告。现在,当广告计数为 2 (insertionFrequency = 2
) 时,它们有时会彼此靠近,但事实并非如此。我怎样才能检测到它并且不让广告出现在彼此附近?
附上截图:
要横向阻止广告,您可以按以下方式进行:
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
if(insertionTargetChildren.eq(random).next('.placeforad').length || insertionTargetChildren.eq(random).prev('.placeforad').length)
//check whether its previous li or next li has ad with class .placeforad
{
i--; //decrease the count so as to come up with separate random number
}
else
insertionTargetChildren.eq(random).after(insertionTemplate);
}
但对于垂直预防,因为每行中的方块数量不同,例如第一行和第二行有3个方块,最后一行只有2个方块,横向识别非常棘手。如果正方形的数量相等,那么也可以采取一些方法来实现这一点..
更新
由于您同意连续获取 3 个块,因此以下代码将避免在水平 以及垂直[=14 上获取广告=]
一个小DEMO
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
var currentElement=insertionTargetChildren.eq(random);
var addedElement=$('li.placeforad:visible');
if(currentElement.next('.placeforad').length || currentElement.prev('.placeforad').length)
{
i--;
}
else
{
if(i!=0)
{
if(addedElement.index() > currentElement.index() &&
(addedElement.index() - currentElement.index())==3)
{
i--;
}
else if(currentElement.index() - (addedElement.index()-1)==3)
{
i--;
}
else
{
insertionTargetChildren.eq(random).after(insertionTemplate);
}
}
else
insertionTargetChildren.eq(random).after(insertionTemplate);
}
}
jQuery(document).ready(function(){
var insertionTemplate = jQuery(".hiddenAdBox").eq(0).html(),
insertionTarget = jQuery('ul'),
insertionTargetChildren = insertionTarget.find('li'),
insertionFrequency = 2;
var random;
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
insertionTargetChildren.eq(random).after(insertionTemplate);
}
});
我有项目列表,还需要在刷新页面时随机显示广告。现在,当广告计数为 2 (insertionFrequency = 2
) 时,它们有时会彼此靠近,但事实并非如此。我怎样才能检测到它并且不让广告出现在彼此附近?
附上截图:
要横向阻止广告,您可以按以下方式进行:
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
if(insertionTargetChildren.eq(random).next('.placeforad').length || insertionTargetChildren.eq(random).prev('.placeforad').length)
//check whether its previous li or next li has ad with class .placeforad
{
i--; //decrease the count so as to come up with separate random number
}
else
insertionTargetChildren.eq(random).after(insertionTemplate);
}
但对于垂直预防,因为每行中的方块数量不同,例如第一行和第二行有3个方块,最后一行只有2个方块,横向识别非常棘手。如果正方形的数量相等,那么也可以采取一些方法来实现这一点..
更新
由于您同意连续获取 3 个块,因此以下代码将避免在水平 以及垂直[=14 上获取广告=]
一个小DEMO
for (var i = 0; i < insertionFrequency; i++) {
random = Math.floor(Math.random() * insertionTargetChildren.length) + 0;
var currentElement=insertionTargetChildren.eq(random);
var addedElement=$('li.placeforad:visible');
if(currentElement.next('.placeforad').length || currentElement.prev('.placeforad').length)
{
i--;
}
else
{
if(i!=0)
{
if(addedElement.index() > currentElement.index() &&
(addedElement.index() - currentElement.index())==3)
{
i--;
}
else if(currentElement.index() - (addedElement.index()-1)==3)
{
i--;
}
else
{
insertionTargetChildren.eq(random).after(insertionTemplate);
}
}
else
insertionTargetChildren.eq(random).after(insertionTemplate);
}
}