根据屏幕的哪一侧向元素添加 class

Add class to element depending on which side of the screen its on

我正在制作一个时间线主题,需要为屏幕右侧和屏幕左侧的帖子分别设置 class。我没有发现我的 javascript 有任何问题,但它当然不起作用。

var halfScreenWidth = jQuery(document).width() * 0.5;
var timelinePost = jQuery('.blog .post');

jQuery(document).ready( function(){
    timelinePost.each( function(){
       if( this.position().left < halfScreenWidth ){
           jQuery(this).addClass('timeline-left');
       } else{
           jQuery(this).addClass('timeline-right');
       }
    });
});

https://jsfiddle.net/zLv4s0ur/26/ 做了一些修改。

if( this.position().left < halfScreenWidth )

应该是

if( jQuery(this).position().left < halfScreenWidth )