[英]Replace <div> with php contents using javascript (no jQuery) based on the browser viewport
[英]Can I replace the contents of a <div> with an external snippet of HTML using JQuery or JavaScript when viewport changes size?
<html>
<head>
</head>
function () {
var viewportWidth = $(window).width();
if (viewportWidth < 700) {
$('#wrapper').load('A.html');
};
<body>
<div id="wrapper">
<h1>original</h1>
</div>
</body>
</html>
我認為類似此功能的東西可能只用A.html就能做到
<h1>small</h1>
但我不知道。
感謝您提供的任何幫助
嘗試使用resize事件:
$(window).resize(function() {
var viewportWidth = $(window).width();
if (viewportWidth < 700) {
$('#wrapper').load('A.html');
};
});
是的,您可以這樣做。請看以下代碼片段:
$(document).ready(function() {
$(window).resize(function() {
var widthCurr=$( window ).width();
if(widthCurr<=360 && widthCurr>276)
{
$('#sample-div').html( "<p>All new content. <em>You bet!</em></p>" );
}
else if(widthCurr<=276)
{
$('#sample-div').html( "<p>I am smaller. <em>You bet!</em></p>" );
}
else if(widthCurr>360)
{
$('#sample-div').html( "<p>Back to original</p>" );
}
});
});
在上面的示例中,隨着窗口寬度的變化,您更改了div的內容。 同樣,您可以設置高度條件,要獲得高度,您需要使用:
var viewportHeight = $(window).height();
最后
$(window).resize
窗口/視口的大小更改時觸發。
另外,如果要更改的內容很小,則應使用.html
而不是.load
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.