- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个商店页面,其中不同的产品是 div 元素,其位置在使用以下 jQuery 加载页面时随机化:
var docHeight = $(document).height(),
docWidth = $(document).width(),
$div = $('#div'),
divWidth = $div.width(),
divHeight = $div.height(),
heightMax = docHeight - divHeight,
widthMax = docWidth - divWidth;
$div.css ({
left: Math.floor( Math.random() * widthMax ),
top: Math.floor( Math.random() * heightMax )
});
我在页面左侧和底部也有 boostrap 导航栏,div 在固定底部栏后面“生成”并允许滚动而不重叠,所以那里没有问题。
我的两个问题是:
1) div 重叠并互相遮挡。我认为解决方案是将先前生成的 div 的位置和大小考虑到下一个生成的 div 中,但我不知道如何将其放入代码中。
[已解决] 2) div 偶尔会出现在左侧导航栏的顶部,与它重叠并阻碍链接。我认为解决方案是在 docWidth = $(document).width() 计算中为 div 指定一个容器以在其中生成或排除导航栏的宽度,但再一次,我我对 jQuery 太陌生,无法解决这个问题。
我制作了这个 jsfiddle https://jsfiddle.net/j1bpt34t/重复运行代码演示了问题。导航栏显示在示例的顶部,但在网站上它固定在左侧。
感谢您的帮助!
编辑:我通过计算容器的宽度解决了问题二:
var $div = $('.navbar-fixed-left'),
navWidth = $div.width();
然后从 widthMax 中减去它,同时将它添加到最后的左侧计算中,如下所示:
widthMax = docWidth - divWidth - navWidth;
left: Math.floor( Math.random() * widthMax + navWidth),
本质上是将生成区域移动导航栏的宽度。
最佳答案
如果您不希望它们重叠,您可以摆脱绝对位置并添加一个 float :左,在继承的位置:静态你不会得到任何重叠。将要随机化的属性从 top 和 left 更改为 margin-top 和 margin-left。你也可以玩 position: relative 如果你需要一个 z-index 但你可以在那里得到一些重叠取决于。
如果您需要随机顺序和/或您只是不想花时间在没有绝对位置的情况下让它看起来像您想要的那样;您可以一次定义所有变量并相互检查它们,或者更好地在循环中检查它们,并且可能是数组中的变量。检查将类似于以下内容:
var docHeight = $(document).height(),
docWidth = $(document).width(),
divWidth = $('#whiteTee').width(),
divWidth2 = $('#blackTee').width(),
divHeight = $('#whiteTee').height(),
heightMax = docHeight - divHeight,
widthMax = docWidth - divWidth,
widthMax2 = docWidth - divWidth2,
randWidth = Math.floor(Math.random() * widthMax),
randWidth2 = Math.floor(Math.random() * widthMax2),
checkFunction1;
checkFunction1 = function(randWidth, randWidth2){
if(randWidth2 < randWidth){ /*THIS IS THE TRICKY PART, finding what in
your requirements for the if will be, you'll probably more likely need to
add your width in with the random number, and check that the second width
and second random number doesn't land it in the area of the other
container, going to get really tricky when you need to check all of them
against eachother. I mean you can keep copy pasting all your if
requirements, it will just be alot of things to check against.*/
randWidth2 = Math.floor(Math.random() * widthMax2);
return randWidth2;
}
else{
randWidth2 = checkFunction1(randWidth, randWidth2);
return randWidth2;
}
}
randWidth2 = checkFunction1(randWidth, randWidth2);
$('#whiteTee').css({
left: randWidth)
});
$('#blackTee').css({
left: randWidth2),
});
完全按照您想要的方式进行操作可能需要大量的 javascript 工作或大量的 CSS 工作,通常最好在 CSS 中完成所有繁重的工作。
另外,为了您自己的阅读(或者至少对于那些回答您的堆栈溢出的人),在使用 jquery 时不要在您的变量中使用 $。
此外,您不断重新定义 docHeight 和 docWidth,它们看起来应该真正定义一次。所有这些 var 声明都在同一范围级别,因此您只需要第一个中的 var,但我的建议是声明它们并立即检查它们,然后执行所有 $(selector).css (css object assignment) 最后的东西,所以它会摆脱它。但是为了您以后的编码,所有这些变量在第一个变量之后看起来都是不必要的,因为它们都是相同的变量。
我没有对此进行测试,也没有实际为您解决您的问题,但希望这能让您了解需要采取的方向。
哦,澄清一下,我的 if 语句 block 是在它位于您想要的区域时,else block 是在它重叠并需要重新分配时。
关于javascript - 使随机定位的 div 彼此和侧边栏不重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179354/
我想创建一个类似 ABOVE 的可折叠侧边栏。 在折叠的表单中,侧边栏将只有选项图标,当您将鼠标悬停在栏上时,它会展开,向您显示图标的描述/名称。 如果您单击该图标,它将带您进入该图标指定的功能,例如
由于我需要在右侧打开侧边栏,如何将其从左侧更改为右侧?我使用的代码很简单: if revealViewController() != nil { Menubutton.target
我在使用高度标签时遇到了问题。 我想制作一个背景延伸到页面底部的侧边栏。 我已将高度设置为 100%,但这并不像我想象的那样有效。 这是我的侧边栏代码。 #side{ box-sizing:b
我正在为我的网站构建一个过滤器侧边栏,我希望能够通过按 btn 打开和关闭过滤器列表。 jQuery('.parent > .children').parent().click(function()
我将如何使 CONTENT HERE 文本进入红色框区域。我几乎尝试了所有我能想到的方法,但没有任何效果。我什至尝试将文本排成一行,然后将其类设置为“行向左拉”。这是我的代码:
当然 Wordpress 2.7 里有“小工具”选项也能控制 Widget ,但是反复实验后发现样式无法统一,可控性较低。个人认为是比较鸡肋的功能,这里就不谈了。 进入正题。侧边栏
我正在为使用 Qt 和 QML for Ubuntu Linux 的应用程序构建 UI。我有一个带有 Canvas 元素的查看器窗口,默认情况下应该是全屏的。打开应用程序时效果很好(即隐藏了 Ubun
我有一个两列布局。左侧是正文,右侧是侧边栏 ( float: right; width: 30% )。 在小型浏览器上,我想先显示正文,然后是侧边栏。但是如果我删除 float从带有媒体查询的侧边栏中
我是 React 的新手,试图让这个组件工作:https://github.com/balloob/react-sidebar 它有效,除了在跟随链接时侧边栏不会关闭。如何单击侧边栏链接关闭侧边栏?
我开始使用 Sublime Text 2。但是有一个小问题,找不到将侧栏(文件夹平移)移动到窗口右侧的选项。 你能告诉我我该怎么做吗?是否有任何插件或设置选项。 最佳答案 遗憾的是,不支持此功能。 看
在下面的玩具示例中,我在侧边栏中有很多 slider 。对于最后一个,我再也看不到正确的情节了。这个问题有什么不涉及删除 slider 的解决方案吗? # 01-kmeans-app palette(
我想向 Finder 侧边栏添加一个新项目。我发现 Finder 将“地点”列表保存在 `~/Library/Preferences/com.apple.sidebarlists.plist 中。我能
我正在寻找创建一个固定侧边栏,与苹果的(就功能而言)非常相似。当您在他们的商店中配置您的 Mac 时,当您向下滚动页面时,右侧的购物篮会固定位置; 完美。 position:fixed 是相对于视口(
我想使用 SwiftUI 构建一个非常简单的 iOS 14 侧边栏。 设置很简单,我有三个 View HomeView , LibraryView和 SettingsView和一个代表每个屏幕的枚举。
Semantic-UI 预先警告其 sidebar page “当侧边栏出现时,固定位置内容可能无法更改其位置。”然后它提供了两种可能的解决方案。 令人惊讶的是,在同一个页面和整个网站上,Semant
有人用过 React Pro 侧边栏吗?有人可以告诉我如何使用 sass 侧边栏组件以及如何增加侧边栏的长度。是否还有其他库提供了如何使用其侧边栏的示例。这是 React Pro 侧边栏示例。
有人用过 React Pro 侧边栏吗?有人可以告诉我如何使用 sass 侧边栏组件以及如何增加侧边栏的长度。是否还有其他库提供了如何使用其侧边栏的示例。这是 React Pro 侧边栏示例。
我是 React 的新手,一直在尝试寻找一个示例,说明如何在 reactjs 中创建侧边导航栏并将 reactrouter 与它一起使用。现在,我有一个如下所示的“侧边导航” Pane : impor
我正在尝试制作一个像 semantic-ui.com 上那样的简单侧边栏,只是我希望它位于右侧。看起来很简单,除了让按钮附加是我最大的问题。你可以在 jsfiddle 看到它,有点工作......HT
我附上了我的 HTML 和 CSS,希望有人能帮助我。基本上我有一个右侧边栏 div,其中的内容不会推到顶部。当我尝试使用位置和高度属性时,内容只是漂浮在整个页面上,甚至不会停留在右侧边栏中。我希望有
我是一名优秀的程序员,十分优秀!