Javascript 动态填充div不可滚动
我是网络世界的新手,所以我正在开发一个简单的网站来学习一些东西,所以我有一个javascript函数来填充一个div:Javascript 动态填充div不可滚动,javascript,html,css,Javascript,Html,Css,我是网络世界的新手,所以我正在开发一个简单的网站来学习一些东西,所以我有一个javascript函数来填充一个div: function printdiv(url, img, title, price, city, position) { var redirect = url; function loadScript() { window.location.href = redirect; } va
function printdiv(url, img, title, price, city, position) {
var redirect = url;
function loadScript() {
window.location.href = redirect;
}
var div = document.createElement("div");
div.style.height = "25vh";
//div.style.overflowY = "auto";
div.style.width = "600px";
div.style.background = "#F2F2F2";
div.style.border = "3px dashed #666633";
div.classList.add('position_'+position);
div.style.position = "fixed";
div.style.top = position+"%";
div.style.left = "25%";
div.onclick = loadScript;
div.innerHTML += '<img src="'+ img +'" width="200" height="137"/>';
div.innerHTML += '<h3 style="float: right; position: absolute; top:0; right:0; margin-top: 1px; ">' + title +'</h3>';
div.innerHTML += '<h4 style="float: right; position: absolute; top:0; right:0; margin-top: 85px; padding-right:55px;">PREZZO:'+ ' ' + price +'</h4>';
div.innerHTML += '<h4 style="float: right; position: absolute; top:0; right:0; margin-top: 110px; padding-right:50px;">CITTÀ:'+ ' ' + city +'</h4>';
document.getElementById("main").appendChild(div);
document.getElementById('main').setAttribute("style","height :"+ 30 + position +"px");
}
函数printdiv(url、img、标题、价格、城市、位置){
var redirect=url;
函数loadScript(){
window.location.href=重定向;
}
var div=document.createElement(“div”);
div.style.height=“25vh”;
//div.style.overflowY=“自动”;
div.style.width=“600px”;
div.style.background=“#f2f2”;
div.style.border=“3px虚线#666633”;
div.classList.add('position_'+position);
div.style.position=“固定”;
div.style.top=位置+“%”;
div.style.left=“25%”;
div.onclick=loadScript;
div.innerHTML+='';
div.innerHTML+=''+标题+'';
div.innerHTML+='PREZZO:'+''+价格+'';
div.innerHTML++='CITTÀ:'+''+city+'';
document.getElementById(“main”).appendChild(div);
document.getElementById('main').setAttribute(“样式”,“高度:”+30+位置+“px”);
}
上述代码填充以下分区:
<div class="main" id="main"></div>
我在一个循环中执行这个函数,以便在“main”div中添加多个位置不同的div,但即使滚动条可见,它也不起作用。
如果我向div添加css背景,滚动条将正确滚动背景,但“main”div中所有动态添加的div将保持锁定并进入页面
在写这个问题之前,我已经尝试过多个与stackoverflow类似的主题,但都没有效果,所以你能给我一个帮助吗?:) 为了通过滚动使DIV溢出,必须将#main DIV设置为相对,而不要将child DIV设置为fixed
function printdiv(url, img, title, price, city, position) {
var redirect = url;
function loadScript() {
window.location.href = redirect;
}
var div = document.createElement("div");
div.style.height = "25vh";
//div.style.overflowY = "auto";
div.style.width = "600px";
div.style.background = "#F2F2F2";
div.style.border = "3px dashed #666633";
div.classList.add('position_'+position);
div.style.top = position+"%";
div.style.left = "25%";
div.onclick = loadScript;
div.innerHTML += '<img src="'+ img +'" width="200" height="137"/>';
div.innerHTML += '<h3 style="float: right; position: absolute; top:0; right:0; margin-top: 1px; ">' + title +'</h3>';
div.innerHTML += '<h4 style="float: right; position: absolute; top:0; right:0; margin-top: 85px; padding-right:55px;">PREZZO:'+ ' ' + price +'</h4>';
div.innerHTML += '<h4 style="float: right; position: absolute; top:0; right:0; margin-top: 110px; padding-right:50px;">CITTÀ:'+ ' ' + city +'</h4>';
document.getElementById("main").appendChild(div);
document.getElementById('main').setAttribute("style","height :"+ 30 + position +"px;position:relative;overflow:scroll;");
}
函数printdiv(url、img、标题、价格、城市、位置){
var redirect=url;
函数loadScript(){
window.location.href=重定向;
}
var div=document.createElement(“div”);
div.style.height=“25vh”;
//div.style.overflowY=“自动”;
div.style.width=“600px”;
div.style.background=“#f2f2”;
div.style.border=“3px虚线#666633”;
div.classList.add('position_'+position);
div.style.top=位置+“%”;
div.style.left=“25%”;
div.onclick=loadScript;
div.innerHTML+='';
div.innerHTML+=''+标题+'';
div.innerHTML+='PREZZO:'+''+价格+'';
div.innerHTML++='CITTÀ:'+''+city+'';
document.getElementById(“main”).appendChild(div);
document.getElementById('main').setAttribute(“样式”,“高度:+30+位置+”px;位置:相对;溢出:滚动;”);
}