轮播图使用

#前端  (引入bootstrap)
<div class="lunbotu">
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicators -->
                        <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>

                        <!-- Wrapper for slides -->
                        <div class="carousel-inner" role="listbox">
                            <div class="item active">
                                <a href=""><img class="banner" src="" alt="..."></a>
                                <div class="carousel-caption">
                                </div>
                            </div>
                            <div class="item">
                                <a href=""><img class="banner" src="" alt="..."></a>
                                <div class="carousel-caption">
                                </div>
                            </div>
                            <div class="item">
                                <a href=""><img class="banner" src="" alt="..."></a>
                                <div class="carousel-caption">
                                </div>
                            </div>
                        </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>
                </div>
#script
#通过ajax的get请求向后端要数据
#$(function () {}这个事件是等所有的页面加载完成才执行,是$(document).ready(function()的简写
$(function () {
    $.ajax({
        url:'/get_banner/',
        method:'get',
        success:function (data) {
            $.each(data,function (k,v) {
                $('.banner')[k].src = v.img_url
                $($('.banner')[k]).parent().attr('href',v.url)
                $($('.banner')[k]).parent().next().html(v.msg)
            })
        }
    })
})
#后端
def get_banner(request):
    l1 = [
        {'url':'http://hsgwh.huangshan.gov.cn/',
         'img_url':'/static/img/banner1.jpg',
         'msg':'<h3>黄山</h3><p>值得玩味的地方</p>'},
        {'url': 'http://www.liriver.com.cn/',
         'img_url': '/static/img/banner2.jpg',
         'msg': '<h3>桂林</h3><p>桂林山水甲天下</p>'},
        {'url': 'http://www.mount-tai.com.cn/layout_class_service_pic.aspx?id=222',
         'img_url': '/static/img/banner3.jpg',
         'msg': '<h3>泰山</h3><p>五岳之首</p>'},
    ]
    return JsonResponse(l1,safe=False)
##做成列表是为了可以后期增删改
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。