- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用下面的代码使我的风景 (4:3) 照片适合轮播。但我想根据照片(横向或纵向)更改 .carousel 的宽度和高度。我该怎么做?
html {
height: 100%;
width: 100%;
}
body {
height: 100%;
width: 100%;
display: block;
}
.carousel {
/* the percentages below are for a 4:3 landscape photo(1600x1200) */
height: 60%;
width: 70%;
}
/* 我需要设置高度:70%;和宽度:纵向 60% */
我应该向轮播元素添加一个类来指示它是风景还是肖像照片?
最佳答案
为纵向创建一个类,为横向创建一个类。当图像加载或当您获得图像大小时,然后确定它是纵向还是横向,然后将适当的类添加到图像或轮播容器。
// list of images - as requested you can put this list in a separate js file
// make sure it is before the other code below
var imagesArray = ["https://lorempixel.com/300/500/animals/1", "https://lorempixel.com/300/500/animals/2", "https://lorempixel.com/500/300/animals/1","https://lorempixel.com/500/300/animals/2","https://lorempixel.com/500/300/city/1","https://lorempixel.com/300/500/city/2"];
// when the user clicks the random button
// we get a random image from our list of URLS
// and then set that as the source of the image
function displayImage(direction, isURL) {
var image = document.getElementById("myImage");
var label = document.getElementById("loadingLabel");
var list = imagesArray.slice(); //make a copy
var currentURL = image.src;
var currentIndex;
var index = 0;
var numberOfImages = list.length;
if (isURL==true) {
currentURL = direction;
}
currentIndex = list.indexOf(currentURL);
if (direction=="next") {
index = currentIndex>=list.length-1 ? 0 : currentIndex+1;
}
else if (direction=="previous") {
index = currentIndex<=0 ? list.length-1 : currentIndex-1;
}
else if (direction=="random") {
list.splice(currentIndex,1);
index = Math.floor(Math.random()*list.length);
}
else if (direction=="start") {
index = 0;
}
else if (direction=="end") {
index = list.length-1;
}
else if (isURL) {
if (currentIndex==-1) {
console.log("Image not found in images array. Check the URL");
return;
}
index = currentIndex;
}
else {
console.log("Direction not specified");
}
image.src = list[index];
label.innerHTML = "Loading " + list[index] + "...";
label.title = list[index];
updateNavigationLabel();
}
// this handles when the image has finished loading
// we check if the image is portrait or landscape
// if it is landscape we set the landscape class
// if it is portrait we set the portrait class
function imageLoadHandler(event) {
var image = document.getElementById("myImage");
var carousel = document.getElementById("myCarousel");
var label = document.getElementById("loadingLabel");
var width = image.naturalWidth;
var height = image.naturalHeight;
var isPortrait = width<height;
var isSquare = width==height;
carousel.classList.remove("portrait");
carousel.classList.remove("landscape");
var caption = width + "x" + height;
if (isPortrait) {
caption = "Portrait (" + caption + ")";
carousel.classList.add("portrait");
}
else if (isPortrait==false) {
caption = "Landscape (" + caption + ")";
carousel.classList.add("landscape");
}
image.caption = caption;
label.innerHTML = caption;
updateNavigationLabel();
}
function updateNavigationLabel() {
var image = document.getElementById("myImage");
var label = document.getElementById("navigationLabel");
var list = imagesArray.slice(); //make a copy
var numberOfImages = list.length;
var currentURL = image.src;
currentIndex = list.indexOf(currentURL);
label.innerHTML = currentIndex+1 +" of " + numberOfImages;
}
window.addEventListener("DOMContentLoaded", function() {
var element = document.getElementById("myImage");
var button = document.getElementById("button");
var carousel = document.getElementById("myCarousel");
// listen for when an image loads
element.addEventListener("load", imageLoadHandler);
// listen for when the user clicks on the random button
button.addEventListener("click", function() {
displayImage('random')
});
// Options - load an image when the page loads
// displayImage("start"); // use to load the first image
// displayImage("end"); // use to load the last image
// displayImage("random"); // use to load a random image
// displayImage("specified", "https://lorempixel.com/300/500/animals/2"); // use to load an image in the images array
displayImage("https://lorempixel.com/300/500/animals/2", true);
});
.landscape {
height: 60%;
width: 70%;
outline:2px solid blue;
}
.portrait {
height: 70%;
width: 60%;
outline:2px solid purple;
}
#myCarousel {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
#myImage {
position: absolute;
left: 50%;
transform: translateX(-50%);
outline: 1px dashed red;
height: 100%;
width: 100%;
object-fit: contain;
}
#button {
position: fixed;
right: 10px;
top: 50px;
}
#loadingLabel {
position: absolute;
bottom: -20px;
left: 50%;
transform: translateX(-50%);
font: 10px sans-serif;
white-space: nowrap;
}
#navigationLabel {
font: 10px sans-serif;
}
#navigation {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
font: 10px sans-serif;
}
<!-- optionally set images in separate file. order before the main javascript -->
<script src="myimages.js"></script>
<div id="myCarousel" class="landscape">
<img id="myImage">
<label id="loadingLabel"></label>
</div>
<button id="button">random</button>
<div id="navigation">
<button id="prev" onclick="displayImage('previous')">prev</button>
<label id="navigationLabel"></label>
<button id="next" onclick="displayImage('next')">next</button>
</div>
关于html - 根据横向或纵向照片切换 .carousel 高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57547792/
我用 Slick 创建了一个垂直旋转木马。我对设计进行了一些更改,除了第一个元素在旋转木马的视野中之外,一切都按我想要的方式工作。一次有 4 个项目在我的轮播中可见,但是当第一个元素位于四个项目中的第
例如,此问题主要发生在用户使用速度较慢的互联网或移动设备时。但是,当轮播中的页面内容/图像没有被缓存时,有时也会在更高的速度上看到闪烁。 如果页面加载并呈现第一个元素,所有轮播“幻灯片”就像一个列表一
当 Owl Carousel 在隐藏元素中启动,然后通过触发器显示该元素时,轮播项目的宽度完全错误(直到窗口调整大小): Fiddle (点击红色链接,看到损坏的布局出现,然后调整窗口大小) 这个问题
在构建轮播时,我意识到猫头鹰添加了克隆的重复项。我的猫头鹰配置看起来像这样。我如何阻止这种情况发生。 owlDfe.owlCarousel({ loop: false,
我正在为一家房地产公司开发一个网站,但我在使用 Bootstrap 的 Carousel 控件时遇到了问题。 我的目标是在一行中包含三个不同的列,并且每列中有一个 Carousel。我让它工作,问题是
我需要让我的 bootstrap 3 carousel-caption 占据我的一半 carousel,让 caption background-color 有点透明,并在其中写入一些文本。 所以逻辑
我正在使用引导媒体 slider 轮播。但是,如果项目少于 4 个,它会重复项目。我想每次点击滑动一个项目。 在下面查看我的代码 查询 $(document).ready(function()
关闭。这个问题需要 details or clarity 。它目前不接受答案。 想改进这个问题?添加详细信息并通过 editing this post 澄清问题。 4年前关闭。 社区在 29 天前审查
似乎默认情况下,Owl Carousel 将代表每张幻灯片的点放在旋转木马下方,而不是底部的内部。我发现我可以给它们添加负边距,它们会进入旋转木马,但它们落后于它有效地使它们变得无用。我想不出让它们出
基于本页 codrops 的代码:Codrops article ,我想将产品 slider 添加到 Bootstrap 轮播中。 I have added a codepen here 这是使瓶子动
我使用 React Bootstrap 轮播。指示器按钮未正确对齐,尺寸也不对。有办法解决这个问题吗? http://imgur.com/a/lhxMS
我很难弄清楚如何使用轮播,更具体地说是如何设置当前幻灯片。 这是文档 of the component 这是 fiddle Go First Go Second
Bootstrap 是否支持在 Carousel 中显示网格列或行而不是 carousel-item -s?这个想法是有一个复杂的网格列结构,它们可以像轮播元素一样相互交换。例如,如果我们有以下网格:
我正在使用 angular-ui bootstrap carousel 并想更改带有图像的左右导航链接。似乎在 tpl 中,它们被硬编码为 ‹ 和 › 有没有人对将这些更改为图像而不更改实际的 ang
React Slick 有没有办法在 Carousel 中包含 Carousel? import Slider from "react-slick";
如何用新的替换/重建 owl 轮播 项目? 描述 在许多情况下,例如当您从数据库中获取新数据时,您会在轮播中添加新数据,但在本示例中,我尝试用新数据替换所有项目。 $('.owl-carousel')
我在我的网站中实现了 Owl Carousel 2,我希望能够使用像这样提供的 Jumbpto 帮助程序跳转到特定幻灯片, $('.btnJump').click(function(){ $(
在轮播上开始拖动时,我无法在移动设备上滚动页面。因此,如果轮播占据了移动设备上的所有可见空间 - 向上/向下翻到页面会变得有点棘手...... 我如何禁用这种滚动的“拦截”? 最佳答案 将此 CSS
我在使用 Twitter Bootstrap Carousel 插件上的 .carousel(number) 方法时遇到问题。不知何故我无法让它工作。轮播本身已启动并正在运行,.carousel('n
我知道之前有人问过这个问题,但是相关的帖子在方法上有所不同,而且 fiddle 的例子也被删除了.. 我的情况:我在这里下载了整个 Bootstrap 表单: http://getbootstrap.
我是一名优秀的程序员,十分优秀!