本章將用模態(tài)窗和動畫的搭配,實現單頁面圖片展示的功能。

這更" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網站運營 > Django搭建網絡相冊:模態(tài)與動畫

Django搭建網絡相冊:模態(tài)與動畫

時間:2023-05-31 13:42:02 | 來源:網站運營

時間:2023-05-31 13:42:02 來源:網站運營

Django搭建網絡相冊:模態(tài)與動畫:如何展示圖片的細節(jié)有很多種方法。作為一個小巧的相冊網站,不需要給每張照片單獨的詳情頁面,而是用浮窗的形式就足夠了。

本章將用模態(tài)窗和動畫的搭配,實現單頁面圖片展示的功能。

這更多的是模板部分的內容。

懸浮動畫

動畫是產品精致程度的體現,好的動畫可以讓用戶感覺非常舒適。

隨著前端技術的發(fā)展,僅靠 css 也可以實現相當精美的動畫,并且有很多現成可參考的代碼。

比如下面這段鼠標懸停后,元素懸浮的動畫。

在項目根路徑新建 /static/hover.css 文件,寫入:(直接復制即可,暫時不用深究細節(jié))

/* /static/hover.css *//* 此樣式來自 Hover.css *//* https://ianlunn.github.io/Hover/ *//* Float Shadow */.hvr-float-shadow {display: inline-block;vertical-align: middle;-webkit-transform: perspective(1px) translateZ(0);transform: perspective(1px) translateZ(0);box-shadow: 0 0 1px rgba(0, 0, 0, 0);position: relative;-webkit-transition-duration: 0.3s;transition-duration: 0.3s;-webkit-transition-property: transform;transition-property: transform;}.hvr-float-shadow:before {pointer-events: none;position: absolute;z-index: -1;content: '';top: 100%;left: 5%;height: 10px;width: 90%;opacity: 0;background: -webkit-radial-gradient(center, ellipse, rgba(0, 0, 0, 0.35) 0%, rgba(0, 0, 0, 0) 80%);background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.35) 0%, rgba(0, 0, 0, 0) 80%);/* W3C */-webkit-transition-duration: 0.3s;transition-duration: 0.3s;-webkit-transition-property: transform, opacity;transition-property: transform, opacity;}.hvr-float-shadow:hover, .hvr-float-shadow:focus, .hvr-float-shadow:active {-webkit-transform: translateY(-5px);transform: translateY(-5px);/* move the element up by 5px */}.hvr-float-shadow:hover:before, .hvr-float-shadow:focus:before, .hvr-float-shadow:active:before {opacity: 1;-webkit-transform: translateY(5px);transform: translateY(5px);/* move the element down by 5px (it will stay in place because it's attached to the element that also moves up 5px) */}css 文件與媒體文件類似,都屬于靜態(tài)文件,Django 默認是不處理它們的。

因此需要修改全局配置,指定此類文件的存放路徑:(就是 hover.css 的路徑)

# /album/settings.py...# 修改STATIC_URL = '/static/'# 新增STATICFILES_DIRS = ( BASE_DIR / 'static',)接著同樣的,將其注冊到 url 路徑中:

# /album/urls.py...urlpatterns = [ ...]urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)然后就可以像下面這樣,在 base.html 中引入它了:

<!-- /templates/base.html -->... <head> ... {% load static %} <link rel="stylesheet" href="{% static 'hover.css' %}"> </head>...使用也很簡單,你想要哪個元素帶有鼠標懸停懸浮效果,在元素標簽里加 class="hvr-float-shadow" 就可以了。

像這樣修改 list.html

<!-- /templates/photo/list.html -->...{% for photo in photos %}<div class="..."> <div class="... hvr-float-shadow"> ... </div></div>{% endfor %}...刷新頁面并將鼠標懸停在圖片卡片上,它就有向上騰空的動畫了。

如果無此效果,檢查瀏覽器控制臺是否有 404 報錯。有就表示靜態(tài)文件載入失敗。

模態(tài)窗

模態(tài)窗是指平鋪在頁面上的一個窗口,并且?guī)в斜尘罢谡值男Ч?。Bootstrap 自帶的模態(tài)窗就非常夠用了。

繼續(xù)修改 list.html 代碼對應位置:

<!-- /templates/photo/list.html -->...{% block content %}...{% for photo in photos %}<div class="col-4 py-2"> <div class="card hvr-float-shadow"> <!-- 修改了這里 --> <a href="#" data-bs-toggle="modal" data-bs-target="#photo-{{ photo.id }}" > <img src="{{ photo.image.url }}" alt="" class="card-img" > </a> <!-- ******* --> </div></div>{% endfor %}...<!-- 新增了這里 --><!-- 注意下面這部分代碼不能直接寫在卡片的 for 循環(huán)中 -->{% for photo in photos %}<!-- Modal --><div class="modal fade" id="photo-{{ photo.id }}"> <div class="modal-dialog modal-dialog-centered modal-lg"> <div class="modal-content"> <div class="modal-body"> <img src="{{ photo.image.url }}" alt="" class="card-img" > </div> </div> </div></div>{% endfor %}<!-- ************ -->{% endblock content %}保存后刷新頁面,點擊圖片,漂亮的模態(tài)窗就滑動出來了:







總結

本章的內容比較簡單,代碼量不多但是視覺效果是很好的,這就是動畫的魅力。

模態(tài)窗的優(yōu)點是頁面不會跳轉,這使得你的網站使用起來連續(xù)性非常好。跳轉使用戶感覺煩躁,并且說不定某次跳轉時就離開你的頁面了。另一方面,模態(tài)窗適合展示較少的信息,大量信息還是老老實實跳轉頁面吧。

點贊 or 吐槽?來評論區(qū)!

關鍵詞:相冊,網絡

74
73
25
news

版權所有? 億企邦 1997-2025 保留一切法律許可權利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關閉