Jquery 在页脚不工作之前停止固定位置
我有一个固定位置的元素:Jquery 在页脚不工作之前停止固定位置,jquery,css,css-position,Jquery,Css,Css Position,我有一个固定位置的元素: .woocommerce #content div.product div.summary { position: fixed; right: 15%; padding: 20px; background-color: #ccc; width: 25%; } 我要做的是让元素在页脚之前停止,我尝试了以下方法: $(document).scroll(function (){ if($('.woocommerce #conten
.woocommerce #content div.product div.summary {
position: fixed;
right: 15%;
padding: 20px;
background-color: #ccc;
width: 25%;
}
我要做的是让元素在页脚之前停止,我尝试了以下方法:
$(document).scroll(function (){
if($('.woocommerce #content div.product div.summary').offset().top + $('.woocommerce #content div.product div.summary').height() >= $('footer').offset().top - 10){
$('.woocommerce #content div.product div.summary').css('position', 'absolute');
}
if($(document).scrollTop() + window.innerHeight < $('footer').offset().top){
$('.woocommerce #content div.product div.summary').css('position', 'fixed');
}
});
$(文档)。滚动(函数(){
如果($('.woocommerce#content div.product div.summary').offset().top+$('.woocommerce#content div.product div.summary').height()>=$('footer').offset().top-10){
$('.woocommerce#content div.product div.summary').css('position','absolute');
}
if($(document).scrollTop()+window.innerHeight<$('footer').offset().top){
$('.woocommerce#content div.product div.summary').css('position','fixed');
}
});
但是我得到了一些奇怪的行为,比如,它一直延伸到顶部,元素的宽度变得更小,我不知道如何修复它
这是我的网页,我正在努力做到这一点
避免页脚越过该div.summary的一种方法是在页脚上添加一个边距top,即该div的高度
#footer {
margin-top: 500px;
}
避免页脚越过该div.summary的一种方法是在页脚上添加一个边距top,即该div的高度
#footer {
margin-top: 500px;
}
Edit=目前正在重新考虑您的意图,您是否从链接中删除了有问题的代码
删除所有Javascript,这对于您试图实现的目标来说是完全不必要的:)
它不会“停留”在那里的原因是因为你在头版上没有更多的帖子,这些主题在完全激活后可以正常工作
但显然,你们不想在主页上看到更多内容是公平的。
将这三个属性添加到类中:
woocommerce#内容部分.产品部分.摘要(主题CSS第720行)
确保将它们放在类中的最后一个,或者删除双重属性
你可以看到原来它的宽度是:49.9%;可能是为了有第二个相同类型的盒子并排放置。大多数现代浏览器最终自行生成正确的全宽,但显然并不理想,最好声明为100%
相对位置和底部0px将负责将其保持在其父分区内
希望这对你有用
ps:始终优先使用可缩放解决方案(%,vw,vh,…)而不是固定大小(像素),稍后当您意识到并非所有浏览器都是平等创建的时,您会感谢我:)
编辑=目前正在重新考虑您的意图,您是否从链接中删除了有问题的代码
删除所有Javascript,这对于您试图实现的目标来说是完全不必要的:)
它不会“停留”在那里的原因是因为你在头版上没有更多的帖子,这些主题在完全激活后可以正常工作
但显然,你们不想在主页上看到更多内容是公平的。
将这三个属性添加到类中:
woocommerce#内容部分.产品部分.摘要(主题CSS第720行)
确保将它们放在类中的最后一个,或者删除双重属性
你可以看到原来它的宽度是:49.9%;可能是为了有第二个相同类型的盒子并排放置。大多数现代浏览器最终自行生成正确的全宽,但显然并不理想,最好声明为100%
相对位置和底部0px将负责将其保持在其父分区内
希望这对你有用
ps:始终优先使用可缩放解决方案(%,vw,vh,…)而不是固定大小(像素),稍后当您意识到并非所有浏览器都是平等创建的时,您会感谢我:)
如果我没弄错的话,这对我来说是一份完美的工作。使用IO,可以对与其他元素或视口重叠的元素作出反应。您可以定义何时触发此观察者,何时开始/结束交叉点,甚至在此之前 要使用IO,首先必须设置要触发IO的时间选项。我没有100%理解你的问题,但是如果你想让元素在页脚之前停止,你需要将根设置为页脚
let options = {
root: document.querySelector('footer'),
rootMargin: '0px',
threshold: 1.0
}
let observer = new IntersectionObserver(callback, options);
然后指定要监视与页脚相交的元素:
let target = document.querySelector('.woocommerce #content div.product div.summary');
observer.observe(target);
最后一步是定义元素重叠时调用的函数:
let callback = (entries, observer) => {
entries.forEach(entry => {
// Each entry describes an intersection change for one observed
// target element
});
};
您也可以使用它来支持较旧的浏览器如果我理解正确的话,这是一个非常适合您的工作。使用IO,可以对与其他元素或视口重叠的元素作出反应。您可以定义何时触发此观察者,何时开始/结束交叉点,甚至在此之前 要使用IO,首先必须设置要触发IO的时间选项。我没有100%理解你的问题,但是如果你想让元素在页脚之前停止,你需要将根设置为页脚
let options = {
root: document.querySelector('footer'),
rootMargin: '0px',
threshold: 1.0
}
let observer = new IntersectionObserver(callback, options);
然后指定要监视与页脚相交的元素:
let target = document.querySelector('.woocommerce #content div.product div.summary');
observer.observe(target);
最后一步是定义元素重叠时调用的函数:
let callback = (entries, observer) => {
entries.forEach(entry => {
// Each entry describes an intersection change for one observed
// target element
});
};
您也可以使用它来支持较旧的浏览器请检查您的计算。计算元素高度和窗高的正确范围。 您的“固定”定位元素应该是根级别。(跨浏览器问题) 在此页上运行代码TargetElementSelector在本例中是页脚。您需要更改它。
(function() {
var mainElement = "main_element_id";
var targetElementSelector = "#footer";
jQuery(targetElementSelector).height(500); // if the footer's height is low to work with
var mainElementMaxTopPosition = jQuery(targetElementSelector).offset().top - jQuery(targetElementSelector).height();
var html = '\
<div id="' + mainElement + '" \
style="position: fixed;width: 250px;height: 250px;left: 0;right: 0;margin: auto;top: 0;bottom: 0;\
background: red;z-index: 99;border: 1px solid black;">\
</div>';
jQuery('body').prepend(html);
jQuery(window).scroll(function() {
var current = jQuery(window).scrollTop() + (jQuery(window).height() / 2);
if (current >= mainElementMaxTopPosition) {
var topPosition = (jQuery(targetElementSelector).offset().top - jQuery('#' + mainElement).height());
jQuery('#' + mainElement).css({
position: "absolute",
bottom: "auto",
top: topPosition + "px"
});
} else {
jQuery('#' + mainElement).css({
position: "fixed",
bottom: "0",
top: "0"
});
}
});
})();
(函数(){
var mainlelement=“main\u element\u id”;
var targetElementSelector=“#footer”;
jQuery(targetElementSelector).height(500);//如果页脚的高度较低,则无法使用
var mainElementMaxTopPosition=jQuery(targetElementSelector).offset().top-jQuery(targetElementSelector).height();
var html='1〕\
\
';
jQuery('body').prepend(html);
jQuery(窗口).滚动(函数(){
var current=jQuery(window.scrollTop()+(jQuery(window.height()/2);
如果(当前>=mainElementMaxTopPosition){
顶位变量