简体   繁体   English

材质设计和jQuery,滚动不起作用

[英]Material design and jQuery, scroll not working

I am unable to make use of the .scroll method of jQuery while including Google's Material design. 在包含Google的Material设计时,我无法使用jQuery的.scroll方法。 I have used Material Design Lite to make navigation bar of site. 我已经使用Material Design Lite制作了网站的导航栏。

If I exclude/remove material.min.js , then scroll method on window works perfectly. 如果我排除/删除material.min.js ,那么window上的scroll方法将完美地工作。 My simple jQuery code is this: 我简单的jQuery代码是这样的:

jQuery(window).scroll(function () {
  console.log("scrolled");
});

Here is the JSFiddle http://jsfiddle.net/wwjoxtvp/2/ 这是JSFiddle http://jsfiddle.net/wwjoxtvp/2/

And here is codepen: http://codepen.io/MustagheesButt/full/PqBYop/ 这是codepen: http ://codepen.io/MustagheesButt/full/PqBYop/

How can I get jQuery working without removing material design? 如何在不删除材料设计的情况下使jQuery工作? Maybe some conflict is occurring, but console is not showing anything. 也许正在发生一些冲突,但控制台未显示任何内容。

Basically you should bind the scroll event to the .mdl-layout__content class since material design lite is making that element scrollable. 基本上,应该将scroll事件绑定到.mdl-layout__content类,因为材料设计精简版使该元素可滚动。

$('.mdl-layout__content').on('scroll', function () {
  console.log('scrolled');  
});

dark4p solved it, but a possible alternative is to use: dark4p解决了它,但是可能的替代方法是使用:

window.addEventListener('scroll', function(){ console.log('scrolled'); }, true);

The true indicating to use capturing rather than bubbling handling so it will still fire. 表示使用捕获而不是冒泡处理的true指示,因此仍会触发。 I'm not sure whether this could have any negative interactions with material, though. 我不确定这是否会与材料产生负面影响。

.mdl-layout__content have overflow-y:auto and you scroll on this div. .mdl-layout__content具有溢出-y:auto,您可以在此div上滚动。 If you want you can change this but i don't recommend this. 如果您愿意,可以更改此设置,但我不建议这样做。

jQuery(document).ready(function(){

    jQuery('.mdl-layout__content').scroll(function(){
console.log('scrolled');
});

});

http://jsfiddle.net/wwjoxtvp/34/ http://jsfiddle.net/wwjoxtvp/34/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM