1

我网站的登录页面 ( http://www.chrisamaddeo.com/ ) 具有全屏背景。我想让它像这个网站标题(http://www.kaiserair.com/)的一个例子一样移动我有这个代码,HTML,它位于我网站的头部:

    <!DOCTYPE html>
    <html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
var movementStrength = 25;
var height = movementStrength / $(window).height();
var width = movementStrength / $(window).width();
$("#top-image").mousemove(function(e){
          var pageX = e.pageX - ($(window).width() / 2);
          var pageY = e.pageY - ($(window).height() / 2);
          var newvalueX = width * pageX * -1 - 50;
          var newvalueY = height * pageY * -1 - 25;
          $('#top-image').css("background-position", newvalueX+"px     "+newvalueY+"px");
});

我有这个代码,css,它不起作用:

#top-image {
background:url('https://d3ui957tjb5bqd.cloudfront.net/images/screenshots/products/0/8/8905/red-rocks-park-o.jpg' -50px -25px;
position:fixed ;
top:0;
width:100%;
z-index:0;
}

我的网站由 weebly 托管,他们的 html 有点不同

4

3 回答 3

3

实际上,我刚刚尝试复制您的代码: http ://codepen.io/chrisboon27/pen/rEDIC

它确实有效,因为它移动了图像。我确实必须在您的 jQuery 中添加一些右大括号,但是当您将代码粘贴到这个问题中时,您可能只是错过了这些。

另外,我查看了您的网站,发现您目前正在使用 background-size:cover。这将获取您的 bg 图像并使其适合 div - 您不希望这样,因为您想要一些 bg 超出 div - 所以在我链接到的示例中的 css 中,我可以看到我使用 calc 来制作 bg-图像大小为 100% 宽度 + 50px。我使用 50px 作为您的代码当前将背景图像位置向左或向右移动最多 25px,因此您需要它比 div 总宽 50px。

编辑:如果您使用 calc,您也应该包含一个 -webkit 前缀版本(除非您使用无前缀或 prefixr 添加前缀。这是浏览器支持:http ://caniuse.com/calc 如果您需要支持更多浏览器您需要通过 javascript 设置背景大小

于 2013-05-24T21:44:34.623 回答
2

如果不重新创建它并查看这些数字的来源,我发现很难遵循这一点。然而,我最近为其他人做了一些与 jQuery 非常相似的东西,只是它在 div 中移动了一个 img。不过,将其切换为移动背景图像可能并不需要太多(尽管找出 bg 图像的尺寸会很棘手——在这种情况下你可能会更好地对它们进行硬编码)。

html:

<div class="container"><img src="foo.jpg" class="draggable"/>
</div>

jQuery:

//for each item
$(".container").each(function(){
  //get the container width
  var conWidth = $(this).width();
  //and its height
  var imgHeight = $(this).find("img").height();
  //get the nested img width
  var conHeight = $(this).height();
  //and its height
  var imgWidth = $(this).find("img").width();
  //figure out how much of the image is not visible horizontally
  var excessWidth = imgWidth - conWidth;
  //and how much is not visible vertically
  var excessHeight = imgHeight - conHeight;
  //how far is this container from the left of the page
  var containerPositionLeft = this.offsetLeft;
  //and from the top
  var containerPositionTop = this.offsetTop;

  //when moving mouse over container
  $(this).mousemove(function(e){
    //figure out how many pixels the mouse is from the left edge of the page
    var mouseoffLeftPage = e.pageX;
    //and how many from the top edge of the page
    var mouseoffTopPage = e.pageY;
    //figure out how many pixels the mouse is from the left edge of the page
    var mouseoffLeftPx = mouseoffLeftPage - containerPositionLeft;
    //and how many from the top edge of the page
    var mouseoffTopPx = mouseoffTopPage - containerPositionTop;
    //figure out the distance the mouse is from the left edge as a percentage (kind of - all the way to the right equals 1 not 100)
    var mouseoffLeftPercent = mouseoffLeftPx/conWidth;
    //do the same for height
    var mouseoffTopPercent = mouseoffTopPx/conHeight;
    //times the 'percentage' value by the amount of image hidden - so if your conatiner is 200px wide, your image 300px wide nd your mouse is half way across this value would be 0.5*100 which would give you 50 - which is exactly half the amount of image that is missing.
    //note this gets set as a minus value as we will be using a minus number to shift the image around.
    var setnewWidth = -(mouseoffLeftPercent * excessWidth);
    //do the same for the height
    var setnewHeight = -(mouseoffTopPercent * excessHeight);
    //add the values as css (using transform(translate) as it's more performant and does subpixel rendering so looks smoother [or does it? it does in animations but seems as my js is not technically animating it it might not make a difference in that respect] - could set the top,left version as fallback for unsupporting browsers but ie9 supports transforms anyway so i dont care.)
    $(this).find("img").css({"transform" : "translate("+ setnewWidth+"px ,"+setnewHeight+"px)" });
    //$(this).find("img").css({"left" : setnewWidth+"px", "top" : setnewHeight+"px" });
  }); 

});

不是对代码中不起作用的直接答案,而是显示了一个示例(对正在发生的事情进行评论)如何完成 - 请注意,我的版本不依赖于您知道任何宽度或高度对象,并且可以在一个页面上的多个项目上运行 - 它也不必放置在页面的最顶部。它确实假设图像比它的容器大 - 如果它不是更大的图像只是在其中移动。您可以通过连续进行更多计算来减少变量的数量,我只是希望它尽可能易于阅读逻辑。

演示: http ://codepen.io/chrisboon27/pen/BhkJq

于 2013-05-24T21:16:15.917 回答
0

比这更简单,您只需确保 CSS 位置设置为“固定”,因此无论您在页面上的哪个位置,它始终是距顶部 0px 的位置。

于 2014-07-02T19:53:15.213 回答