jquery - 即使我滚动,中间的 CSS 图像

标签 jquery html css

我这里有这个页面:http://paoladi.com/designers/

然后我添加了这段代码:

<style type="text/css">

    .entry-content img { display: none; float:right; position: fixed; top: 36%; left: 30%; }
    .entry-content p{ width:30%; font-weight:bold; margin-bottom:0px;}

    </style>

即使我滚动,图像也始终位于页面中间,但是我的屏幕很小并且在我的屏幕上看起来很好,我只是想知道这在大屏幕上看起来如何,我可以做些什么来改进这段代码吗? jQuery 是一个选项吗?

要查看图片,请将鼠标悬停在“360 毛衣”上

谢谢, J

最佳答案

如果知道图片大小,可以在css中使用calc()动态计算位置。

在你的例子中,如果图片宽度为200px,高度为100px;

<style type="text/css">

.entry-content img { display: none; float:right; position: fixed; top: calc(50% - 50px); left: calc(50% - 100px); }
.entry-content p{ width:30%; font-weight:bold; margin-bottom:0px;}

</style>

请参阅 this page 中的一些详细信息

此外,如果需要,您还可以使用 javascript 或 jQuery 来更新位置。例如,将 .resize() 事件添加到 $(window)

$(window).resize(function(){

    $(".entry-content img").position({
        left: $('body').width() / 2 - $(".entry-content img").width() / 2,
        right: $('body').height() / 2 - $(".entry-content img").height() / 2,
    })
})

关于jquery - 即使我滚动,中间的 CSS 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21128186/

相关文章:

javascript - 从 HTML 操作 javascript 数组

javascript - jQuery 函数 .on ("click") 在触发类时不起作用

css - 在 CSS 中使用 z-index 时如何避免间隙?

css sub child ul 其 child 的宽度,而不是 parent

javascript - 如何使用 jqgrid 仅使单击的单元格可编辑,而不是单击的行中的所有可编辑单元格?

javascript - 为什么我无法使用 JSON 数据在 JS 函数调用上构建树

javascript - AngularJs - 我如何才能仅在答案正确时显示答案

html - 这是网页还是图片?

html - 两个 CSS 网格在同一个父容器中不对齐

css - CSS颜色如何动画化