javascript - 没有为jquery中的所有容器设置滑块高度?
问题描述
我尝试为每个容器的滑块具有最大高度的所有滑块设置相等的高度,到目前为止它工作正常。但问题是,当我有多个时.container
,高度取自第一个而不是每个,我想根据各自的滑块最大高度为每个容器设置高度。
https://codepen.io/burner/pen/rZWdpN
HTML
<div class="container">
<div id="grouping">
<div class="responsive slider1">
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking.
</div>
<div class="responsive slider2">
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
</div>
<div class="responsive slider3">
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
</div>
</div>
</div>
<div class="container">
<div id="grouping1">
<div class="responsive slider4">
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
</div>
<div class="responsive slider5">
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
</div>
<div class="responsive">
<div class="slider6">
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking
</div>
</div>
</div>
</div>
CSS
.container, .container2 {
display: relative;
margin-top: 2em;
}
.responsive {
display: inline-block;
width: 100px;
vertical-align: top;
background-color: green;
color: white;
}
JS
function settingSliderHeight(){
var $maximumHeight = 0;
var $actualSlider = null;
$('.container .responsive').each(function() {
var $sliderHeight = parseFloat($(this).outerHeight());
if ($sliderHeight > $maximumHeight) {
$maximumHeight = $sliderHeight;
console.log("$maximumHeight" + $maximumHeight);
$actualSlider = $(this);
}
$(this).not($actualSlider).css("height", $maximumHeight);
});
$('.container .responsive').not($actualSlider).css("height", $maximumHeight);
}
setTimeout(function(){
settingSliderHeight();
},2000)
解决方案
试试这个 jquery 一次
<script>
function settingSliderHeight(){
var $maximumHeight = 0;
var $actualSlider = null;
$('.container').each(function() {
var $sliderHeight = parseFloat($(this).outerHeight());
if ($sliderHeight > $maximumHeight) {
$maximumHeight = $sliderHeight;
console.log("$maximumHeight" + $maximumHeight);
$actualSlider = $(this);
}
$(this).find(".responsive").css("height", $sliderHeight);
});
}
setTimeout(function(){
settingSliderHeight();
},2000)
</script>
推荐阅读
- android - 如何将 Android.Graphics.Bitmap 转换为 System.Drawing.Bitmap 并返回?
- solr - java.lang.NullPointerException\n\tat org.apache.solr.schema.TrieField.readableToIndexed(TrieField.java:433)
- vue.js - Vue.elementDirective 不是函数
- r - 在 R 中,如何在绘图的两个内部函数之间传递数据帧/小标题,然后保存绘图?
- php - 表单邮件安全
- javascript - 如何使用 puppeteer 访问一个接一个的 url
- reactjs - 在 React 中为 Month 类型的输入设置最小值
- laravel - 方法 Laravel\Passport\Bridge\AccessToken::__toString() 不能抛出异常,升级后捕获 InvalidArgumentException 错误
- python - 将多面体几何转换为列表
- linux - awk 与两个模式的 AND 条件在不匹配的行之间丢失