javascript - How to create new slide item in owl carousel dynamically
问题描述
I want to create owl item slides dynamically on the basis of conditions. Such as
if(i=1 && i<=4)
{
//generate new item slide (owl carousel)
}
else if(i>=4 && i<=8)
{
//Generate second slide
}
I am creating the slide item manually, i want to make them dynamically on the basis of condition in jquery. Suppose the first condition met, create new slide item dynamically, if second condition met, create new slide dynamically. I am new at jquery
Ps:- slide should create dynamically with the help of jquery .
Here is slider code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="myjs2.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<link rel="stylesheet" href="style2.css">
<style>
body
{
font-family:Arial;
font-size : 10pt;
padding:15px;
}
.ui-datepicker-calendar {
display: none;
}
</style>
<title>yes</title>
</head>
<body>
<div class="test"></div>
<div id="owl-demo" class="owl-carousel owl-theme">
<div class="item">
first slide
</div>
<div class="item">
second slide
</div>
<div class="item">
Third slide
</div>
</div>
<script>
$(document).ready(function() {
$("#owl-demo").owlCarousel({
navigation : true, // Show next and prev buttons
slideSpeed : 300,
paginationSpeed : 400,
items : 1,
itemsDesktop : false,
itemsDesktopSmall : false,
itemsTablet: false,
itemsMobile : false
});
});
</script>
</body>
</html>
解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="myjs2.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<link rel="stylesheet" href="style2.css">
<style>
body
{
font-family:Arial;
font-size : 10pt;
padding:15px;
}
.ui-datepicker-calendar {
display: none;
}
</style>
<title>yes</title>
</head>
<body>
<div class="test"></div>
<div id="owl-demo" class="owl-carousel owl-theme">
<div class="item"> 1 slide </div>
<div class="item"> 2 slide </div>
<div class="item"> 3 slide </div>
</div>
<button id="addCarous"> Add Carousel</button>
<script>
$(document).ready(function() {
$("#owl-demo").owlCarousel({
navigation : true, // Show next and prev buttons
slideSpeed : 300,
paginationSpeed : 400,
items : 1,
itemsDesktop : false,
itemsDesktopSmall : false,
itemsTablet: false,
itemsMobile : false
});
$("#addCarous").click(function(e){
debugger;
var number = document.getElementsByClassName("item").length +1;
e.preventDefault(); //-- prevent form submit
$('#owl-demo').trigger('add.owl.carousel', ['<div class="item"> '+number+' slide </div>'])
.trigger('refresh.owl.carousel');
});
});
</script>
</body>
</html>
It is an optional parameter. It specifies the function that returns the content to insert. Index: It returns the index position of the element in the set. HTML: It returns the current HTML of the selected element. It's just like appending the tag inside the (carousel).
$("#id").trigger('add.owl.carousel',[])
Events are provided by Owl Carousel in strategic code locations. This gives you the ability to listen for any changes and perform your own actions. You could also trigger events by yourself to control Owl Carousel
initialize.owl.carousel Type: attachable Callback: onInitialize When the plugin initializes.
initialized.owl.carousel Type: attachable Callback: onInitialized When the plugin has initialized.
resize.owl.carousel Type: attachable Callback: onResize When the plugin gets resized.
resized.owl.carousel Type: attachable Callback: onResized When the plugin has resized.
refresh.owl.carousel Type: attachable, cancelable, triggerable Callback: onRefresh Parameter: [event, speed] When the internal state of the plugin needs update.
refreshed.owl.carousel Type: attachable Callback: onRefreshed When the internal state of the plugin has updated.
drag.owl.carousel Type: attachable Callback: onDrag When the dragging of an item is started.
dragged.owl.carousel Type: attachable Callback: onDragged When the dragging of an item has finished.
translate.owl.carousel Type: attachable Callback: onTranslate When the translation of the stage starts.
translated.owl.carousel Type: attachable Callback: onTranslated When the translation of the stage has finished.
change.owl.carousel Type: attachable Callback: onChange Parameter: property When a property is going to change its value.
changed.owl.carousel Type: attachable Callback: onChanged Parameter: property When a property has changed its value.
next.owl.carousel Type: triggerable Parameter: [speed] Goes to next item.
prev.owl.carousel Type: triggerable Parameter: [speed] Goes to previous item.
to.owl.carousel Type: triggerable Parameter: [position, speed] Goes to position.
destroy.owl.carousel Type: triggerable Destroys carousel.
replace.owl.carousel Type: triggerable Parameter: data Removes current content and add a new one passed in the parameter.
add.owl.carousel Type: triggerable Parameter: [data, position] Adds a new item on a given position.
remove.owl.carousel Type: triggerable Parameter: position Removes an item from a given position.
To use Owl Carousel, you’ll need to make sure both the Owl and jQuery 1.7 or higher scripts are included.You don't need any special markup. All you need is to wrap your divs(owl works with any type element) inside the container element . Class "owl-carousel" is mandatory to apply proper styles that come from owl.carousel.css file.
推荐阅读
- arrays - VBA 数组逆
- reactjs - 如何在 React 项目中自动创建 Sentry 版本并将源映射上传到 Sentry?
- c# - ASP.NET Core 2.1 RedirectToAction 将 TempData 附加到 url
- selenium-webdriver - 使用 gojs 实现 UI 自动化
- arrays - 如何分隔列中的值并计算出现次数?
- mips - 如何获取 MIPS 中常量的地址?
- mongodb - 客户端 - 代理信息报告错误:“DEVICE_GROUP_NOT_FOUND”
- ios - AVPlayerItemVideoOutput copyPixelBuffer 总是返回 1280x720
- tensorflow - 为什么具有相似 mAP 的两个推理模型在部署中具有完全不同的性能?
- excel - 使用函数打开和更新外部工作簿中的值,但返回源错误