- Bootstrap 4:Responsive Web Design
- Silvio Moreto Matt Lambert Benjamin Jakobus Jason Marah
- 391字
- 2021-07-09 18:54:53
Making the menu affix
The affix plugin is present only in version 3 of Bootstrap (it was removed in version 4), and it aims to toggle the position of an element between fixed and relative, emulating the effect of position: sticky
, which is not present in all browsers.
We will apply the sticky effect the left #profile
element although we do not have enough elements to make a scroll on our web page. Therefore, to make it simple, replicate the <li>
in ul#feed
to increase the number of items in the list. Do this three times or more to make a scroll in your web browser.
In div#profile
, add the markup related to affix:
<div id="profile" class="col-md-3 hidden-sm hidden-xs" data-spy="affix" data-offset-top="0">
…
// rest of the profile HTML
</div>
Refresh the web browser. You will see that the affix is not working yet. Since we are making the left column with a fixed position with the affix plugin, it is removing the entire column from the grid, making the columns glitch from left to right.
So, we need a workaround for that. We must create some piece of JavaScript code using the events triggered for the plugin.
Let's use the affix.bs.affix
event, which is an event fired just before the affixing of the element:
$(document).ready(function() { … // rest of the JavaScript code $('#profile').on('affix.bs.affix', function() { $(this).width($(this).width() - 1); $('#main').addClass('col-md-offset-3'); }).on('affix-top.bs.affix', function() { $(this).css('width', ''); $('#main').removeClass('col-md-offset-3'); }); });
Thus, we have played with some tricks in the preceding JavaScript code.
In the first delegated event, .on('affix.bs.affix', handler)
,when the element switches to position: fixed
, we keep the width of the left column. It would change the width because the .col-md-3
class does not have a fixed width; it uses a percentage width.
We also added the offset to the middle column, corresponding to the detached left column, the .col-md-offset-3
class.
The affix-top.bs.affix
event does the opposite action, firing when the element returns to the original top position and removing the custom width and the offset class in the middle column.
To remove the fixed width and return to the .col-md-3
percentage width, just add the $(this).css('width', '')
line. Also remove the .col-md-offset-3
class from the #main
content.
Refresh the web browser, scroll the page, and see the result, exemplified in the next screenshot. Note that the profile is fixed on the left while the rest of the content scrolls with the page:

- Django+Vue.js商城項(xiàng)目實(shí)戰(zhàn)
- JavaScript+jQuery開發(fā)實(shí)戰(zhàn)
- Python測(cè)試開發(fā)入門與實(shí)踐
- QGIS:Becoming a GIS Power User
- 學(xué)Python也可以這么有趣
- Learning AWS
- 精通MySQL 8(視頻教學(xué)版)
- Akka入門與實(shí)踐
- R語言實(shí)戰(zhàn)(第2版)
- 優(yōu)化驅(qū)動(dòng)的設(shè)計(jì)方法
- The Applied Data Science Workshop
- 程序員的英語
- Learning Redux
- JavaScript編程精解(原書第3版)
- SQL Server 2012數(shù)據(jù)庫管理與開發(fā)(慕課版)