2016-12-15 57 views
-6

我有一個網站,我想旋轉我創建的流播放器上的我的會員鏈接。我到處搜索了一個腳本,但找不到一個腳本。我沒有試圖使用爲我做的服務。我想自己編寫腳本。有什麼建議麼?Javascript廣告橫幅旋轉器爲我的網站?

+1

我建議你開始編寫一些代碼,然後提出一個問題時,有具體的問題。 – 4castle

+0

我完全理解你來自哪裏,但那是問題所在。我主要使用C#工作,但是在html和CSS中構建一個站點,並且需要一個橫幅旋轉器。如果我冒犯了,對不起。 – Retro617

回答

0

未來,請嘗試爲自己拍一張照片,並提出更具體的問題。我不明白爲什麼你不想使用服務,如果你不寫它。

但是,既然你是,這是我的建議。

的Javascript:

var banners = [ 
    { 
    "img": "tnl-banner-steve-jobs-01.png", 
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html", 
    "weight": 1 
    }, 
    { 
    "img": "tnl-banner-steve-jobs-02.png", 
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html", 
    "weight": 1 
    }, 
    { 
    "img": "tnl-banner-steve-jobs-03.png", 
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html", 
    "weight": 1 
    }, 
    { 
    "img": "tnl-banner-steve-jobs-04.png", 
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html", 
    "weight": 1 
    }, 
    { 
    "img": "tnl-banner-steve-jobs-05.png", 
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html", 
    "weight": 4 
    } 
]; 

而這裏的腳本添加到您的HTML:

<script> 

// Fetch the banner setup file. 
var filename = getURLParameter("type")+".js"; 
jQuery.getScript(filename, function(){ 
    var banner = randomBanner(); 
    // Add the banner to the page body. 
    $('body').append("<a target=\"tnl_ad\" href=\""+banner["url"]+"\">" + 
    "<img src=\"banners/"+banner["img"]+"\"></a>"); 
}) 
    .fail(function(jqxhr, settings, exception) { 
    console.log("Error parsing " + filename + ": " + exception.message); 
    } 
) 

function randomBanner() { 
    var totalWeight = 0, cummulativeWeight = 0, i; 
    // Add up the weights. 
    for (i = 0; i < banners.length; i++) { 
     totalWeight += banners[i]["weight"]; 
    } 
    console.log("Total weight: " + totalWeight); 
    var random = Math.floor(Math.random() * totalWeight); 
    // Find which bucket the random value is in. 
    for (i = 0; i < banners.length; i++) { 
     cummulativeWeight += banners[i]["weight"]; 
     if (random < cummulativeWeight) { 
      return(banners[i]); 
     } 
    } 
} 

function getURLParameter(name){ 
    return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]); 
} 
</script> 

Source

+0

如果這個問題想要圖書館/教程的建議,這將是脫離主題,因爲這些問題是基於意見的。請不要回答這個問題。它只會鼓勵這種行爲。 – 4castle

+0

今後不要回答一個問題,你需要從'未來開始,嘗試給自己一個鏡頭,並提出更具體的問題。 –

+0

謝謝你的幫助,如果我含糊不清,我很抱歉。我使用了一些javascript,但還不足以解決這個問題。我非常感謝你的幫助。 – Retro617

0

這是一個滑塊/旋轉木馬,將不同的幻燈片之間旋轉一個偉大的js插件(或橫幅在您的案件。)

http://kenwheeler.github.io/slick/

見自動播放功能。

希望它有幫助。

+0

看起來像一個偉大的服務。 @ Retro617,如果你不想自己寫,並且正在尋找外部代碼,我強烈建議使用這個。 – brld

+1

爲什麼選擇投票? – Brad

+0

林不知道,我也有。 – brld