简体   繁体   English

固定div仅垂直与jQuery

[英]fixed div only vertically with jquery

I want fixed an div or image in left of html page, and when scroll y, image scroll, but when scroll x image not scroll or fixed in left, I use jquery.sticky-kit.js and jquery-scrolltofixed.js plugin but when page resize to small width (mobile size) scroll x that, note that my page not responsive. 我想修复html页面左侧的div或图像,当滚动y时,图像滚动,但是当滚动x图像不滚动或左侧时,我使用jquery.sticky-kit.js和jquery-scrolltofixed.js插件,但是当页面调整为小宽度(移动尺寸)时,滚动x即可,请注意我的页面没有响应。

Another note that a mootools plugin is that work well, but I want do this in jquery. 另一个注意事项是,mootools插件可以很好地工作,但是我想在jquery中做到这一点。

Mootools plugin is in this link: http://demo.rickyh.co.uk/css-position-x-and-position-y/ Mootools插件在此链接中: http : //demo.rickyh.co.uk/css-position-x-and-position-y/

HTML: HTML:

<div id="content"></div>

CSS: CSS:

#content {
    width:50px;
    height:50px;
    background:#ccc;
    position:fixed;
    top:50px;
    left:50px;
}

jQuery: jQuery的:

$(window).scroll(function () {

    var content =  $("#content");
    var windowWidth = $(window).width()

    content.css("top",-$(window).scrollTop() + 50) // 50 is initial top position

    // if the window width is smaller than 600px - scroll also x-asis
    if (windowWidth < 600) { 

        content.css("left",-$(window).scrollLeft() + 50) // 50 is initial left position

    } else  {

        content.css("left", "50px") // 50 is initial top position    

    }

});

Fiddle: http://jsfiddle.net/n4we5xma/3/ 小提琴: http : //jsfiddle.net/n4we5xma/3/

Use CSS position property : horizontal fixed ,vertical relative 使用CSS 位置属性:水平固定,垂直相对

.scroll_fixed {
    position:absolute;
}

See a working example 查看工作示例

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

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