2016-08-31 59 views
0

我已經得到了我的圖像使用srcset以下代碼。我寧願完全脫離srcset。我希望這可以處理所有具有特定類別的圖像。jQuery功能,將改變圖像鏈接

<img src="images/Connect-Groups-600.jpg" 
srcset=" 
images/Connect-Groups-600.jpg 600w, 
images/Connect-Groups-1000.jpg 1000w, 
images/Connect-Groups-1920.jpg 1920w, 
images/Connect-Groups-2880.jpg 2880w 
" alt=""/> 

有誰知道使用jquery的解決方案,該解決方案將採用標記並根據屏幕大小更改位置。我也需要這個在移動設備上工作。

例如:

  • 如果屏幕比1920瓦特更大,它使用 圖像/ 2880 /連接-Groups.jpg
  • 如果屏幕比千瓦特和 1920瓦特或更少更大,它將拉圖像/ 1920/Connect-Groups.jpg
  • 如果屏幕介於601和1000之間,它將拉動 images/1000/Connect-Groups.jpg
  • 如果屏幕爲600W或更少,它拉的圖像/ 600 /連接-Groups.jpg

如果尺寸檢測失敗,我希望它永遠拉在標籤中的圖像。

我需要這個工作,不管圖像是什麼...我不知道甚至搜索什麼來尋找解決方案來做到這一點。

編輯

在這個月底,我希望能夠把在網頁上...代碼然後將其拉離img標籤的URL,並追加正確的大小夾。

它最終會看起來像在最終用戶方面。

+0

https://api.jquery.com/resize/還有'$(文件)。就緒()'第一負載執行。這會導致您的圖像在加載文檔之後加載。 – Roberrrt

+0

我很確定使用該方法會導致負載緩慢,如果頁面圖像很重。我更喜歡那些只是改變了圖像位置的圖像,而圖像已經被調整大小,並且只能拉出所需的圖像大小。它仍然必須在小尺寸的移動設備上加載完整的2880圖像,然後才能進行調整大小功能。 – KDJ

+0

我正在這樣做,以加快頁面加載。較小的圖像會比真正大的圖像更快。但我也需要大圖像在大屏幕上填充屏幕 – KDJ

回答

1

請注意,這種方法是可持續的,我會建議使用它爲別的,但它會幫助你解決這個具體問題:

鑑於您的文件夾結構仍然類似於:

圖片/ <size>/<filename.ext>

我們可以重新創建基於此信息來源:

function resize() { 

    // First, we'll grab all the images on the page that can be changed: 
    var img = document.querySelectorAll('img.resizable'); 

    for(var i = 0; i < img.length; i++) { 
     // Grab the image extension 
     var src = img[i].src.split('/') 

     if (window.innerWidth > 1920) { 
      img[i].src = "/images/2880/" + src[src.length - 1]; // Add the file name and extension 
     } 
     else if (window.innerWidth > 1000 && window.innerWidth <= 1920) { 
      img[i].src = "/images/1920/" + src[src.length - 1]; // Add the file name and extension 
     } 
     else if (window.innerWidth > 601 && window.innerWidth <= 1000) { 
      img[i].src = "/images/1000/" + src[src.length - 1]; // Add the file name and extension 
     } 
     else if (window.innerWidth <= 600) { 
      img[i].src = "/images/600/" + src[src.length - 1]; // Add the file name and extension 
     }  
    } 

} 

resize(); // execute when document is loaded to insert manual images for the first time without resizing 

而且ofcourse,加載基本600寬度的文件爲默認值。

例jQuery中:

function resize() { 

    var img = $('img.resizable'); 
    var width = $(window).innerWidth(); 

    img.each(function(index, element) { 

     var name = element.src.split('/') // Split is a native javascript function, which 'splits' the string into an array with the components 

     if(width <= 600) { 
      $(element).attr('src', 'images/600/' + name[name.length - 1]) // This name[name.length -1] trick is being used to select the 'last value in the string' based on the length of the string. 
     } 
     else if(width <= 1000) { 
      $(element).attr('src', 'images/1000/' + name[name.length - 1]) 
     } 
     else if(width <= 1920) { 
      $(element).attr('src', 'images/1920/' + name[name.length - 1])   
     }   
     else { 
      $(element).attr('src', 'images/2880/' + name[name.length - 1])   
     } 

    }) 

} 

resize(); 
+0

是這個使用javascript或jQuery?看起來像是javascript:/另外,你認爲不可持續的意思是什麼?你對這個腳本的關注是什麼?是否有更好的方法? – KDJ

+1

1)它不使用jQuery,它完全是vanilla 2)這是不好的做法。如果某人編輯你的代碼並發現沒有CSS用於「可調整大小」並將其刪除?如果你的老闆決定你需要另一個斷點呢?這個代碼在這個問題上是不可持續或強大的。但是,再一次,也許我只是非常懷疑。我剛剛完成了幾個月的'基於場景的編碼',其中我們必須創建對於幾乎每個用戶都是萬無一失的代碼。 – Roberrrt

+0

或者如果開發部門的某個人突然有'Eureka!'會怎麼樣?時刻並將圖像文件夾重命名爲'img /'? – Roberrrt

0

[修改] 讓這就是你的形象標籤

<img class="imageid" src="images/Connect-Groups-600.jpg"> 

那麼這就是你正在尋找希望

var images = document.getElementsByClassName("imageid"); 
 
var sizes = ["images/2880/Connect-Groups.jpg","images/1920/Connect-Groups.jpg","images/1000/Connect-Groups.jpg","images/600/Connect-Groups.jpg"]; 
 
window.onresize = resize; 
 

 
function resize() { 
 
    console.log(window.innerWidth); 
 
    if (window.innerWidth > 1920) { 
 
    images[0].src = sizes[0]; 
 
    } else if (window.innerWidth > 1000 && window.innerWidth <= 1920) { 
 
    images[0].src = sizes[1]; 
 
    } else if (window.innerWidth > 601 && window.innerWidth <= 1000) { 
 
    images[0].src = sizes[2]; 
 
    } else if (window.innerWidth <= 600) { 
 
    images[0].src = sizes[3]; 
 
    } 
 
}
<img id="imageid" src="images/Connect-Groups-600.jpg">
的JS

+0

這是純Javascript,如果您需要它,您需要自己處理它,因爲您沒有詢問有關jquery的問題,這就是爲什麼我在javascript中執行此操作的原因。 –

+0

有沒有辦法在JavaScript中做一個數組的大小,並從該數組中拉出。陣列(600,1000,1920,2880)?只是好奇。我很欣賞答案,我會插入並測試它。 – KDJ

+0

也,它需要是一個類...沒有ID ...因爲我想這是用於網站廣泛....如果在同一頁上有多個圖像,那麼它將只能拉一個單圖片。 – KDJ

1

差不多相同穆罕默德的回答,但在jQuery中。如果你想按班去做,只需刪除'#'並添加'。「

window.onresize = resize; 
 

 
function resize() { 
 
    console.log($(window).innerWidth); 
 
    if ($(window).innerWidth > 1920) { 
 
    $("#imageid").src = "images/2880/Connect-Groups.jpg"; 
 
    } else if ($(window).innerWidth > 1000 && $(window).innerWidth <= 1920) { 
 
    $("#imageid").src = "images/1920/Connect-Groups.jpg"; 
 
    } else if ($(window).innerWidth > 601 && $(window).innerWidth <= 1000) { 
 
    $("#imageid").src = "images/1000/Connect-Groups.jpg"; 
 
    } else if ($(window).innerWidth <= 600) { 
 
    $("#imageid").src = "images/600/Connect-Groups.jpg"; 
 
    } 
 
}
<img id="imageid">

+0

它不能使用特定的圖像,因爲這是網站廣泛...所以它需要拉代碼內的圖像url ... – KDJ

+0

大小數組?你什麼意思?圖像源與您需要完成的任務無關。你已經知道你想要根據if語句(> 1920,你訪問2880/..,如果它> 100和<= 1920,你使用1920 /文件夾等)指向什麼圖像文件夾。您可以設置與大小文件夾和圖像源的關鍵值關係,並將其插入。不知道您還有什麼意思。這是處理你想要做的事情的正確方法。 – Omeed

+0

這就是爲什麼我把數組拿出來......我將在稍後考慮修改......但是我不需要那裏的實際鏈接....對不起,編輯人員刪除了部分描述。 – KDJ