简体   繁体   English

如何在图像通过页面上的某个点时更改图像,然后在图像高于该点时将其更改回来?

[英]How do I change an image when it passes a certain point on a page, and then change it back when it is above that point?

I am completely new to javascript, but I want to learn it rather than jQuery to get some fundamental knowledge. 我是javascript的新手,但我想学习它而不是jQuery来获得一些基础知识。 The function I want to create should animate an arrow to turn upwards when it passes 50% of the page. 我想要创建的函数应该在箭头通过页面的50%时动画向上转动。 In addition to this, the function that it calls when upside down should change. 除此之外,倒置时调用的功能应该改变。

So: upArrow onClick(scrollUp) downArrow onClick(scrollDown) 所以:upArrow onClick(scrollUp)downArrow onClick(scrollDown)

I'm sure I sound stupid, but maybe someone can understand what I'm trying to explain. 我确定我听起来很愚蠢,但也许有人能理解我想要解释的内容。

You can either write the imageelement.src property to point to a different arrow, or, for extra smoothness, use a CSS sprite containing both arrows and write element.style.backgroundPosition to change the displayed image without having to load a new image. 您可以将imageelement.src属性写入指向不同的箭头,或者,为了获得额外的平滑度,请使用包含两个箭头的CSS sprite和write element.style.backgroundPosition来更改显示的图像,而无需加载新图像。

You can assign a new function to imageelement.onclick to change what happens when you click on it, but usually it is simpler to have one click-function that decides what to do based on state. 您可以为imageelement.onclick指定一个新功能,以更改单击它时发生的情况,但通常只需一个单击功能即可根据状态决定做什么。

eg: 例如:

<div id="arrow" class="arrow-down"></div>


#arrow {
    position: fixed; top: 0; left: 0; z-index: 10;
    width: 32px; height: 32px;
    background-image: url(/img/arrow.png);
}
.arrow-down {
    background-position: 0 0;
}
.arrow-up {
    background-position: 0 -32px;
}


var arrow= document.getElementById('arrow');

window.onscroll=window.onresize= function() {
    arrow.className= isHalfwayDown()? 'arrow-up' : 'arrow-down';
};
arrow.onclick= function() {
    var h= document.documentElement.scrollHeight;
    scrollTo(0, isHalfwayDown()? 0 : h);
    window.onscroll();
};

function isHalfwayDown() {
    var y= document.documentElement.scrollTop+document.body.scrollTop;
    var h= document.documentElement.scrollHeight;
    return y>=h/2;
}

Browser compatibility notes: window.onscroll() is because IE doesn't fire the scroll event when scrollTo() is used. 浏览器兼容性说明: window.onscroll()是因为IE在使用scrollTo()时不会触发滚动事件。 Both html and body 's scroll offsets have to be added together in isHalfwayDown() because the browsers unfortunately don't agree on which element represents the viewport. htmlbody的滚动偏移都必须在isHalfwayDown()加在一起,因为不幸的是浏览器不同意哪个元素代表视口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 jQuery 将页面滚动到某个点时,如何更改文本? - How can I make text change when a page is scrolled to a certain point using jQuery? 当 window 大小达到 javascript 中的某个点时,如何更改 css - How do you change css when window size gets to a certain point in javascript 当音频在JavaScript中经过某个时间点时运行功能 - Run function when audio passes a certain time point in JavaScript 有没有办法改变我的 <header> 页面到达某个特定锚点时的样式? - Is there a way to change my <header> style when the page reaches certain #anchor point? 当元素达到特定点时更改CSS属性 - Change CSS property when an element reach a certain point 当我单击图像中的超链接点而不在另一个页面上打开它时,如何使此音频剪辑在同一页面上打开? - How do I make this audio clip open on the same page when I click on the hyperlink point in the image without opening it on another page? 如果对象到达页面上的某个点,如何使对象停止? - How do I make an object stop if it reaches a certain point on a page? 当div滚动到某个点以上时更改文本的颜色 - changing color of text when div is scrolled above certain point 当运动图像到达屏幕上的某个点时如何水平翻转运动图像 - How to flip a moving image horizontally when it reaches a certain point on the screen 将上方的div更改为fixed时,如何阻止继承位置的元素跳出页面? - How do I stop an inheritly positioned element from jumping up page when I change the div above it to fixed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM