Bootstrap实现轮播图,套模板就用这个吧!

话不多直说上代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title>Bootstrap 101 Template</title>
		//使用cdn这个才会有左右箭头
		<!-- Bootstrap -->
		<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
		<style>
			//修改轮播图的宽度
			#myCarousel {
				width: 400px;
			}
		</style>

	</head>
	<body>
		<div class="container" id="myCarousel">
			<div id="carousel-example-generic" class="carousel slide">
				<ol class="carousel-indicators">
					<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
					<li data-target="#carousel-example-generic" data-slide-to="1"></li>
					<li data-target="#carousel-example-generic" data-slide-to="2"></li>
				</ol>
				<div class="carousel-inner">
					<div class="item active">
						<img src="imags/bg3.jpg">
						// 在轮播图上加上文字
						<div class="carousel-caption">
							<h3>hello</h3>
						</div>
					</div>
					<div class="item">
						<img src="imags/点赞.jpeg">
					</div>
					<div class="item">
						<img src="imags/1.jpg">
					</div>
					<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
						<span class="glyphicon glyphicon-chevron-left"></span>
					</a>

					<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
						<span class="glyphicon glyphicon-chevron-right"></span>
					</a>
				</div>
			</div>
		</div>
		<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
		<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
		<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
		<script>
			//自定义切换时间 默认为5s
			$('.carousel').carousel({
  			interval: 2000
  		</script>
})
	</body>
</html>

使用轮播图可以让你的网站看起来更加高逼格,试试吧!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Django 实现轮播图可以通过以下步骤来完成: 1. 创建一个 Django app,用于存放轮播图相关的代码。 2. 在 app 创建一个模型类,用于存储轮播图的信息,例如图片地址、标题、描述等等。 3. 在 app 创建一个视图函数,用于渲染轮播图页面。 4. 在模板使用 JavaScript 或 jQuery 插件实现轮播图的效果。 下面是一个大致的实现过程: 1. 创建 app 在 Django 项目创建一个名为 `carousel` 的 app: ``` python manage.py startapp carousel ``` 2. 创建模型类 在 `carousel/models.py` 文件创建一个模型类,用于存储轮播图的信息: ```python from django.db import models class Carousel(models.Model): title = models.CharField(max_length=255) image = models.ImageField(upload_to='carousel_images/') description = models.TextField(blank=True, null=True) def __str__(self): return self.title ``` 这个模型类包含了轮播图的标题、图片和描述信息。 3. 创建视图函数 在 `carousel/views.py` 文件创建一个视图函数,用于渲染轮播图页面: ```python from django.shortcuts import render from .models import Carousel def carousel_view(request): carousel_items = Carousel.objects.all() context = {'carousel_items': carousel_items} return render(request, 'carousel.html', context) ``` 这个视图函数获取了所有的轮播图信息,并将其传递到模板进行渲染。 4. 创建模板 在 `carousel/templates` 目录下创建 `carousel.html` 文件,用于渲染轮播图页面: ```html {% extends 'base.html' %} {% block content %} <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> {% for item in carousel_items %} <li data-target="#carousel-example-generic" data-slide-to="{{ forloop.counter0 }}" {% if forloop.first %}class="active"{% endif %}></li> {% endfor %} </ol> <!-- Wrapper for slides --> <div class="carousel-inner" role="listbox"> {% for item in carousel_items %} <div class="item {% if forloop.first %}active{% endif %}"> <img src="{{ item.image.url }}" alt="{{ item.title }}"> <div class="carousel-caption"> <h3>{{ item.title }}</h3> <p>{{ item.description }}</p> </div> </div> {% endfor %} </div> <!-- Controls --> <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> {% endblock %} ``` 这个模板使用了 Bootstrap 轮播图的 HTML 结构,通过 Django 模板语言循环渲染了轮播图的信息。 5. 配置 URL 在项目的 `urls.py` 文件配置 URL,将 `/carousel/` 请求映射到视图函数 `carousel_view`: ```python from django.urls import path from .views import carousel_view urlpatterns = [ path('carousel/', carousel_view, name='carousel'), ] ``` 这样,当访问 `/carousel/` 时,就会渲染出轮播图页面。 6. 实现轮播图效果 在模板使用 JavaScript 或 jQuery 插件实现轮播图效果。这里以 Bootstrap 轮播图为例,只需要在模板引入 Bootstrap 的 JavaScript 和 CSS 文件,并添加以下 JavaScript 代码即可实现轮播图效果: ```javascript $(document).ready(function(){ $('.carousel').carousel(); }); ``` 这样就完成了 Django 轮播图实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值