javascript - 将图像 slider 高度设置为最小图像的高度

标签 javascript jquery css

我正在使用 http://bxslider.com/用于响应式图像 slider 。 slider 当前将高度设置为最大图像的高度。我想让它成为最小图像的高度,并剪掉比最小图像大的图像的任何多余部分。

我试过 adaptiveHeight: true ,但这会导致 slider 下方的内容随着 slider 调整大小时移动。我尝试使用 css,以下内容几乎可以正常工作。

.bx-wrapper, .bx-viewport {
    height: 300px !important;
}

我在这里遇到的问题是 300 像素看起来非常适合我的屏幕,但我有一个响应式网站,所以当我在较小的屏幕上查看时,图像下方有大量空白区域,因为 300 像素太大了。

最佳答案

为您的应用设置不同的屏幕分辨率。然后使用 @media 查询来更改 CSS:

下面的代码对您的 HTML 做了什么:

  1. 首先,每个浏览器页面都有自己的视口(viewport)大小或窗口大小。
  2. 基于窗口大小的媒体查询。
  3. 如果您的窗口大小在特定范围内,则会应用这些样式。
  4. 为了让您的 BX Slider 响应,我们根据屏幕尺寸调整高度
/* Default Style */
.bx-wrapper, .bx-viewport {
    height: 300px !important;
}
/* Smartphones (portrait and landscape) ----------- */
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
    .bx-wrapper, .bx-viewport {
        height: 250px !important;
    }
}

/* Smartphones (landscape) ----------- */
@media only screen and (min-width : 321px) {
    .bx-wrapper, .bx-viewport {
        height: 275px !important;
    }
}

/* Smartphones (portrait) ----------- */
@media only screen and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
/* Styles */
}
/**********
iPad 3
**********/
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}
/* Desktops and laptops ----------- */
@media only screen  and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen  and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */
}

/* iPhone 5 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* iPhone 6 ----------- */
@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* iPhone 6+ ----------- */
@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* Samsung Galaxy S3 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */
}

/* Samsung Galaxy S4 ----------- */
@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

/* Samsung Galaxy S5 ----------- */
@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */
}

我为前两个做到了。将其保留为模板,您可以在开发者工具中使用 Chrome 的响应式设计测试器在不同的分辨率下进行测试:

Chrome Dev Tools

关于javascript - 将图像 slider 高度设置为最小图像的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30705739/

相关文章:

javascript - 显示一年中每一天对象详细信息的日历

javascript - meteor.js - 临时服务器端应用程序状态

javascript - 动态生成的按钮不适用于 Javascript

ipad - iPad Safari (iOS 5.0.1) 上的 iframe CSS3 缩放问题

javascript - 启动 opentok 视频时创建新的 DOM,无法按下按钮

javascript - 带有雷达循环覆盖的 Google map API v3 绘图,并自动更新雷达

javascript - ajax - 将数据作为 json 发送到 php 服务器并接收响应

javascript - ShowHide (shID) 一次只显示一个 div?

javascript - 是否可以设置组件在文本换行之前可以水平滚动的最大距离?

mfc - 如何获得 GDI HFONT 的行高?