我想在现有脚本中通过 Javascript 更改 HTML div 的位置
目前 This My Existing Javascript 正在前端生成。
<div class="navbar navbar-inverse affix" id="SubMenu" style="top: 72px;">
但是我想定位到top:8px
JavaScript 如下所示。
// Sub-navbar affix on scroll
// ------------------------------------------------------------------------
if ($('#SubMenu').length) {
$('#SubMenu').affix({
offset: {
top: function() {
return $('#SubMenu').parent().offset().top - $('#navbar-main-container').outerHeight();
},
}
}).css('top', $('#navbar-main-container').outerHeight());
// Update values on window resize
$(window).resize(function() {
theTop = $('#SubMenu').parent().offset().top - $('#navbar-main-container').outerHeight();
$('#SubMenu').data('bs.affix').options.offset = {
top: theTop
};
});
$('#SubMenu').on('affixed.bs.affix', function() {
$('a.navbar-brand.scrollTop span').text($('#destination-the-title').val());
});
$('#SubMenu').on('affixed-top.bs.affix', function() {
setTimeout(function() {
$('a.navbar-brand.scrollTop span').text('');
}, 600)
});
}
最佳答案
由于您使用的是 jQuery,只需使用以下代码行即可。
$("#SubMenu").css("top","8px");
更准确地说 相反,如果这部分
.css('top', $('#navbar-main-container').outerHeight());
添加 .css("top","8px");
关于javascript - 在javascript中更改div的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569579/